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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2010
    Posts
    224
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Redirect on Submit question

    I've managed to get a functioning 'Contact Us' form on to a website I'm working on. Everything works EXCEPT the 'Thank you for contacting us. We will be in touch with you very soon'.

    I've used some free code I found, but on hitting Submit I'm just taken to a blank page with the message: "'Thank you for contacting us. We will be in touch with you very soon".

    I would like to have this message displayed on a page within the website, and can easily create a page that will do this, but I don't seem to be able to direct the user to this page when they hit the submit button.

    The php code is as follows, which has a statement at the very bottom asking for the url of this 'Thank you' page to be added, but it doesn't explain in what way to add this url

    Code:
    <?php
    if(isset($_POST['email'])) {
         
        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "you@yourdomain.com";
        $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['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
    }
    ?>
    Where it says: <!-- include your own success html here -->, what does it mean? In what form am I meant to insert the 'success html'?

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    It means as it says; where the comment to include your own html is copy and paste the HTML from your success page, or include the success page by doing
    PHP Code:
    include('my_success_page.php'); 

  • #3
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    You don't have to include a file, you could just use echo to display it
    PHP Code:
    <?php
    if(isset($_POST['email'])) {
         
        
    // EDIT THE 2 LINES BELOW AS REQUIRED
        
    $email_to "you@yourdomain.com";
        
    $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['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 -->
     
    echo "Thank you for contacting us. We will be in touch with you very soon.";
     
    }
    ?>
    I'd love to change the world, but they wont give me the source code.

  • #4
    Regular Coder
    Join Date
    Feb 2010
    Posts
    224
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thank you for the answers.

    Doug, I created a success page and included its html where instructed, but on Submit it kept throwing up the: function died($error) message regarding there being errors on the form that needed correcting. I checked and checked and checked, and I was definitely filling out the form correctly, and yet I continued to get this message.

    jswany, when you say use: echo "Thank you for contacting us. We will be in touch with you very soon.";

    What will the user see when they hit the submit button?

    I would like a duplicate of the contact page to display (obviously minus the contact form) with the message above displayed in its place.

  • #5
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    It would just display the text
    Thank you for contacting us. We will be in touch with you very soon.
    Have you got the whole contact form script? HTML form and PHP script.

    Is the HTML form posting to the mail script or is it on the same page?
    I'd love to change the world, but they wont give me the source code.

  • #6
    Regular Coder
    Join Date
    Feb 2010
    Posts
    224
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jswany View Post
    It would just display the text
    Okay, but that's what it does already (a plain white page with the text in the top left corner of the browser screen). It's functional, but ugly.

    Quote Originally Posted by jswany View Post
    Have you got the whole contact form script? HTML form and PHP script.

    Is the HTML form posting to the mail script or is it on the same page?
    Yes. This is the whole of the html and php. And yes, I am saving the php file as send_form_email.php

    I have uploaded the php separately to the hdocs directory on my server.

    HTML

    Code:
    <form name="contactform" method="post" action="send_form_email.php">
    <table width="450px">
    <tr>
     <td valign="top">
      <label for="first_name">First Name *</label>
     </td>
     <td valign="top">
      <input  type="text" name="first_name" maxlength="50" size="30">
     </td>
    </tr>
    <tr>
     <td valign="top"">
      <label for="last_name">Last Name *</label>
     </td>
     <td valign="top">
      <input  type="text" name="last_name" maxlength="50" size="30">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="email">Email Address *</label>
     </td>
     <td valign="top">
      <input  type="text" name="email" maxlength="80" size="30">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="telephone">Telephone Number</label>
     </td>
     <td valign="top">
      <input  type="text" name="telephone" maxlength="30" size="30">
     </td>
    </tr>
    <tr>
     <td valign="top">
      <label for="comments">Comments *</label>
     </td>
     <td valign="top">
      <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
     </td>
    </tr>
    <tr>
     <td colspan="2" style="text-align:center">
      <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
     </td>
    </tr>
    </table>
    </form>
    PHP

    Code:
     <?php
    if(isset($_POST['email'])) {
         
        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "you@yourdomain.com";
        $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['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 -->
     
    echo "Thank you for contacting us. We will be in touch with you very soon.";
     
    }
    ?>
    Last edited by OurJud; 10-14-2013 at 06:52 PM.

  • #7
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    So if I understand right, you want the form to be displayed on the contact page and then once it has been submitted the success message to be displayed on the contact form without the form?

    If this is the case you're better off putting the PHP code on the contact page then using an if statement to show the form if it has not been submitted or show the success/error message if it has. The below should do this though I haven't tested it.

    Contact.php
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    // check if email form has been submitted.
    if(isset($_POST['email'])) {
         
        
    // EDIT THE 2 LINES BELOW AS REQUIRED
        
    $email_to "you@yourdomain.com";
        
    $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['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 -->
     
        
    echo "Thank you for contacting us. We will be in touch with you very soon.";
     
     
    // if form hasn't been submitted then show form
    } else {

        
    //////// HTML for form
        
    ?>
        <form name="contactform" method="post" action="send_form_email.php">
            <table width="450px">
                <tr>
                    <td valign="top"><label for="first_name">First Name *</label></td>
                    <td valign="top"><input  type="text" name="first_name" maxlength="50" size="30" /></td>
                </tr>
                <tr>
                    <td valign="top"><label for="last_name">Last Name *</label></td>
                    <td valign="top"><input  type="text" name="last_name" maxlength="50" size="30" /></td>
                </tr>
                <tr>
                    <td valign="top"><label for="email">Email Address *</label></td>
                    <td valign="top"><input  type="text" name="email" maxlength="80" size="30" /></td>
                </tr>
                <tr>
                    <td valign="top"><label for="telephone">Telephone Number</label></td>
                    <td valign="top"><input  type="text" name="telephone" maxlength="30" size="30" /></td>
                </tr>
                <tr>
                    <td valign="top"><label for="comments">Comments *</label></td>
                    <td valign="top"><textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center"><input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a></td>
                </tr>
            </table>
        </form>
        <?php

    ?>
    </body>
    </html>
    I'd love to change the world, but they wont give me the source code.

  • #8
    Regular Coder
    Join Date
    Feb 2010
    Posts
    224
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Thank you, jswany, after much faffing I managed it.

    Thanks for all your help, everyone.

    I tried to mark this as 'solved' but I've passed the time for which I can edit my original post.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Glad to hear you've got it done

    Quote Originally Posted by OurJud View Post
    after much faffing I managed it.
    I did warn you it would take you days lol
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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