...

View Full Version : PHP Newbie Query



ninan714
01-24-2012, 09:04 PM
Hi!

I am coding a contact form for my first website. All the work is done, and the contact form appears on the website - result!

However, when I try and send a test e-mail to a different e-mail address, I get the following error:



Parse error: syntax error, unexpected '{' in /home/ninan714/public_html/send_form_email.php on line 1

I have never used PHP before, so I have already done a bit of research to get to this stage, but I am staring at the code until I am blue in the face and I cannot see what the problem is.

I assumed that the error was telling me there was an extra { in the first line, but I've taken that out and it doesn't seem to help.

This is the code I am using:


<?phpif(isset($_POST['email']))
$email_to = "EMAIL ADDRESS (I've just removed it for the purposes of this post!!";
$email_subject = "Website Query";


function died($error) { // your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."

<br />
<br />
"; echo "Please go back and fix these errors.
<br />
<br />
"; die(); } // validation expected data exists if
(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))
{ died('We are sorry, but there appears to be a problem with the form you submitted.'); }
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; }
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; }
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0) { died($error_message); }
$email_message = "Form details below.\n\n"; function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); }
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers); ?>
<!-- include your own success html here --> Thank you for contacting us. We will be in touch with you very soon. <?php?>

Could someone please tell me where I am going wrong?

I'm sorry, this is probably a really stupid question for all those out there who are proper users!

Thanks, Ninan714

mlseim
01-24-2012, 09:11 PM
For every { you need a }

Use Notepad++ (free editor) to view your scripts.
You'll see it highlights ( ) { } and HTML tags.
That will help you find missing tags and brackets.

ninan714
01-24-2012, 09:48 PM
Thank you for this, but I am still stuck!

I have gone through and double checked all my brackets, and that all now seems okay.

However, it is now picking up a syntax error on this line:
[code]died {('We are sorry, but there appears to be a problem with the form you submitted.');}[code]

I don't know what a syntax error is, or why it should occur here. I have played trial and error with the '{' and the '}', moving it to see if it made any difference; but it hasn't.

Are you able to explain what a syntax error is, or what is wrong with the line of code?

Thank you again!

BluePanther
01-24-2012, 09:55 PM
For every { you need a }

Use Notepad++ (free editor) to view your scripts.
You'll see it highlights ( ) { } and HTML tags.
That will help you find missing tags and brackets.

Or Netbeans ;). Love Netbeans, brilliant IDE - will give PHP hints and php manual references for the whole language.

mlseim
01-25-2012, 03:50 AM
Repost it again ... after you've made changes since the last script you posted.

ninan714
01-25-2012, 09:45 PM
Repost it again ... after you've made changes since the last script you posted.

Hi Again

I have tried again, also replacing the command 'died' with 'die', as I have been told that this was incorrect. Here is my amended code, and this time I have a new error!


<?php if(isset($_POST['email']))

{ // EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "EMAIL@gmail.com";
$email_subject = "Website Query";
}

function died($error)

{ echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."

<br />
<br />
"; echo "Please go back and fix these errors.
<br />
<br />
"; die(); }

// validation expected data exists if
(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))


die{("We are sorry, but there appears to be a problem with the form you submitted.");}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from))

{
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; }
$string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name))

{
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; }
if(!preg_match($string_exp,$last_name))

{
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; }
if(strlen($comments) < 2)

{
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; }
if(strlen($error_message) > 0)

{ die($error_message); }

$email_message = "Form details below.\n\n"; function clean_string($string)

{
$bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); }
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers); ?>
<!-- include your own success html here --> Thank you for contacting us. We will be in touch with you very soon. <?php?>

The error that I am now getting is:

Parse error: syntax error, unexpected T_EXIT in /home/ninan714/public_html/send_form_email.php on line 29

I have had a look for similar errors, but most results refer to ensuring brackets are all closed - and I have looked at this until I am blue in the face and I don't think I can see the wood for the trees anymore!

Any help/suggestions would be greatly appreciated!

tracknut
01-25-2012, 09:59 PM
// validation expected data exists if
(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))


die{("We are sorry, but there appears to be a problem with the form you submitted.");}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required


Two errors here, in red.

I the first, you've commented out the "if" that starts the if statement for the next four lines. So pull that 'if' down to the next line:


// validation expected data exists
if (!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))


The second one is just a wacky way to call your 'died' function. You probably want to read up on PHP syntax when you get some time, but it should look like:



died ("We are sorry, but there appears to be a problem with the form you submitted.");


Dave

ninan714
01-25-2012, 10:08 PM
// validation expected data exists if
(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))


die{("We are sorry, but there appears to be a problem with the form you submitted.");}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required


Two errors here, in red.

I the first, you've commented out the "if" that starts the if statement for the next four lines. So pull that 'if' down to the next line:


// validation expected data exists
if (!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))






The second one is just a wacky way to call your 'died' function. You probably want to read up on PHP syntax when you get some time, but it should look like:



died ("We are sorry, but there appears to be a problem with the form you submitted.");


Dave

Thank you so much - you are clearly an amazing programming God!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum