Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Newbie Query

    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:

    Code:
    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:
    Code:
    <?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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • Users who have thanked mlseim for this post:

    ninan714 (01-24-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by mlseim View Post
    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.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Repost it again ... after you've made changes since the last script you posted.

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by mlseim View Post
    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!

    Code:
    <?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!
    Last edited by ninan714; 01-25-2012 at 09:54 PM.

  • #7
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,297
    Thanks
    10
    Thanked 282 Times in 281 Posts
    Code:
    // 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:
    Code:
    // 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:

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

  • Users who have thanked tracknut for this post:

    ninan714 (01-25-2012)

  • #8
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by tracknut View Post
    Code:
    // 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:
    Code:
    // 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:

    Code:
       
            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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •