13 Replies Latest reply: Aug 10, 2012 8:08 AM by manojb05 RSS

    PHP form submission Problem

    manojb05 Community Member

      Dear all,

      PHP enquiry form submission some error is comes

      "Parse error: syntax error, unexpected T_STRING in /home/newtocli/public_html/en45/send_form_email.php on line 11"

      Please help me

      WEBLINK

       

       

      PHP goes like this

       

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

      <title>PHP Form</title>

      </head>

       

       

      <body><?php

      if(isset($_POST['email'])) {

       

          // EDIT THE 2 LINES BELOW AS REQUIRED

          $email_to = "xyz@xyz.co.in";

          $email_subject = "Your email subject line";

       

       

          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['email']) ||

              !isset($_POST['telephone']) ||

                          !isset($_POST['company_name']) ||

              !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

          $email_from = $_POST['email']; // required

          $telephone = $_POST['telephone']; // not required

                $last_name = $_POST['company_name']; // 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,$company_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 .= "Email: ".clean_string($email_from)."\n";

          $email_message .= "Telephone: ".clean_string($telephone)."\n";

                $email_message .= "Company Name: ".clean_string($company_name)."\n";

          $email_message .= "Comments: ".clean_string($comments)."\n";

       

      /* Redirect visitor to the thank you page */

      header('Location:gt.html');

      exit();

       

      // 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

      }

      ?>

      </body>

      </html>

        • 1. Re: PHP form submission Problem
          osgood_ MVP

          2 issues manojb

           

           

          1) You have $last_name instead of $company_name in your php script (should be as below)

           

          $company_name = $_POST['company_name']; // required

           

           

          2) If you position the redirect where you have the form will not send the information as the php stops running at that point and never gets to the @mail command

           

          /* Redirect visitor to the thank you page */

           

          header('Location:gt.html');

           

          exit();

           

          Position it AFTER the @mail command.

           

           

          Also the php script doesnt need the html wrapped around it. It can just go in a document without that.

          • 2. Re: PHP form submission Problem
            manojb05 Community Member

            osgood thanks for your reply. i am go with your recommend now error come like this

             

            Parse error: syntax error, unexpected T_STRING in /home/newtocli/public_html/en45/send_form_email.php on line 4

            • 3. Re: PHP form submission Problem
              osgood_ MVP

              Have your removed the html tags from around the form script?

               

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

              <html xmlns="http://www.w3.org/1999/xhtml">

              <head>

              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

              <title>PHP Form</title>

              </head>

               

               

              <body>

               

               

               

               

              </body>

              </html>

              • 4. Re: PHP form submission Problem
                manojb05 Community Member

                sorry i am testing the code, anyway now error is something like

                 

                Parse error: syntax error, unexpected T_STRING in /home/newtocli/public_html/en45/send_form_email.php on line 11


                • 5. Re: PHP form submission Problem
                  osgood_ MVP

                  If the form works you can turn error reporting off - just add the below after the opening <?php tag

                   

                  error_reporting(0);

                   

                  The form works ok for me.

                  • 6. Re: PHP form submission Problem
                    manojb05 Community Member

                    something wrong from my side i dont know where it is gone wrong now error is

                    Parse error: syntax error, unexpected T_STRING in /home/newtocli/public_html/send_form_email.php on line 12

                    I am really sorry osgood

                     

                     

                    PHP Code Updated

                     

                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                    <html xmlns="http://www.w3.org/1999/xhtml">

                    <head>

                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

                    <title>PHP Form</title>

                    </head>

                     

                     

                    <body><?php

                    error_reporting(0);

                    if(isset($_POST['email'])) {

                     

                        // EDIT THE 2 LINES BELOW AS REQUIRED

                        $email_to = "xyz@rty.co.in";

                        $email_subject = "Your email subject line";

                     

                     

                        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['email']) ||

                            !isset($_POST['telephone']) ||

                                        !isset($_POST['company_name']) ||

                            !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

                        $email_from = $_POST['email']; // required

                        $telephone = $_POST['telephone']; // not required

                              $company_name = $_POST['company_name']; // 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,$company_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 .= "Email: ".clean_string($email_from)."\n";

                        $email_message .= "Telephone: ".clean_string($telephone)."\n";

                              $email_message .= "Company Name: ".clean_string($company_name)."\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); 

                    /* Redirect visitor to the thank you page */

                    header('Location:gt.html');

                    exit();

                    ?>

                     

                    <!-- include your own success html here -->

                     

                    Thank you for contacting us. We will be in touch with you very soon.

                     

                    <?php

                    }

                    ?>

                    </body>

                    </html>

                    • 7. Re: PHP form submission Problem
                      osgood_ MVP

                      You still have not removed the html code from around the php script...try removing that.

                       

                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                      <html xmlns="http://www.w3.org/1999/xhtml">

                      <head>

                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

                      <title>PHP Form</title>

                      </head>

                       

                       

                      <body>

                       

                       

                       

                       

                      </body>

                      </html>

                      • 8. Re: PHP form submission Problem
                        manojb05 Community Member

                        osgood something like this

                         

                        <?php

                        error_reporting(0);

                        if(isset($_POST['email'])) {

                         

                            // EDIT THE 2 LINES BELOW AS REQUIRED

                            $email_to = "xyz@rty.co.in";

                            $email_subject = "Your email subject line";

                         

                         

                            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['email']) ||

                                !isset($_POST['telephone']) ||

                                            !isset($_POST['company_name']) ||

                                !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

                            $email_from = $_POST['email']; // required

                            $telephone = $_POST['telephone']; // not required

                                  $company_name = $_POST['company_name']; // 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,$company_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 .= "Email: ".clean_string($email_from)."\n";

                            $email_message .= "Telephone: ".clean_string($telephone)."\n";

                                  $email_message .= "Company Name: ".clean_string($company_name)."\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);

                        /* Redirect visitor to the thank you page */

                        header('Location:gt.html');

                        exit();

                        ?>

                         

                        <!-- include your own success html here -->

                         

                        Thank you for contacting us. We will be in touch with you very soon.

                         

                        <?php

                        }

                        ?>

                        • 9. Re: PHP form submission Problem
                          osgood_ MVP

                          Yes,

                           

                          I don't know why you are getting the T_String error. If I copy what you paste and test it with your form everything works fine for me.

                          • 10. Re: PHP form submission Problem
                            manojb05 Community Member

                            some error now

                            Parse error: syntax error, unexpected T_STRING in /home/newtocli/public_html/send_form_email.php on line 5

                             

                            My form like this

                             

                            <form name="contactform" method="post" action="send_form_email.php">

                                                   

                                                                                                                                                                        <div class="field text">

                                                                                   

                                                                                    <input  type="text" name="first_name" maxlength="50" size="30" value="Name*:">

                                                                                   

                                                                                    </div>

                                                                                                                                                                        <div class="field text">

                                                                                    <input  type="text" name="email" maxlength="80" size="30" value="E-mail*:">

                                                                                   

                                                                                   

                                                                                   

                                                                                   </div>

                                                                                                  <div class="clear"><!-- --></div>

                                                                                                                                                                        <div class="field text">

                                                                                   

                                                                                    <input  type="text" name="telephone" maxlength="30" size="30" value="Phone.:">

                                                                                   

                                                                                    </div>

                                                                                                                                                                        <div class="field text">

                                                                                   

                                                                                    <input  type="text" name="company_name" maxlength="50" size="30" value="Company:">

                                                                                   

                                                                                    </div>

                                                                                                  <div class="clear"><!-- --></div>

                                                                                                  <div class="field textarea"><div>

                                                                                                    <textarea  name="comments"></textarea>

                                                       

                                                        </div></div>

                                                                                                  <div class="submit">

                                                                                                            <p>* - Required fields</p>

                                                                                                            <input type="submit" value="Send" />

                                                                                                  </div>

                                                                                        </form>


                            • 11. Re: PHP form submission Problem
                              manojb05 Community Member

                              i am save as a php code its working thanks osgood

                              • 12. Re: PHP form submission Problem
                                osgood_ MVP

                                What was it saved as before then manojb?

                                 

                                Your form action says:

                                 

                                send_form_email.php

                                • 13. Re: PHP form submission Problem
                                  manojb05 Community Member

                                  send_form_email.php i am changed file name to send.php its working.