Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 13 of 13

Thread: How To: Redirect the Kirupa "PHP Contact Form"

  1. #1

    How To: Redirect the Kirupa "PHP Contact Form"

    One of the most common questions that people as is how to redirect the "PHP Contact Form". And since people either fail at searching or are just to lazy here's how you do it. Here's the final code from the tutorial:
    PHP Code:
    <?php
    if(isset($_POST['submit'])) {

        
    $to "you@you.com";
        
    $subject "Form Tutorial";
        
    $name_field $_POST['name'];
        
    $email_field $_POST['email'];
        
    $message $_POST['message'];
     
        
    $body "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
     
        echo 
    "Data has been submitted to $to!";
        
    mail($to$subject$body);

    } else {

        echo 
    "blarg!";

    }
    ?>
    change

    PHP Code:
    echo "Data has been submitted to $to!"
    to

    PHP Code:
    header("Location: http://www.yourpage.com"); 
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  2. #2
    Simp - I don't know if it will make a difference but you may want the header change to come after the call to the mail() function just in case the browser redirects before the email gets sent.

  3. #3
    Quote Originally Posted by Yeldarb View Post
    Simp - I don't know if it will make a difference but you may want the header change to come after the call to the mail() function just in case the browser redirects before the email gets sent.
    Yea good suggestion. Didn't really think to much about it I just know this question is asked at least once a week. So for those reading this his suggestion is to turn:

    PHP Code:
        header("Location: http://www.yourpage.com");  
        
    mail($to$subject$body); 
    to

    PHP Code:
        mail($to$subject$body); 
        
    header("Location: http://www.yourpage.com"); 
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  4. #4
    Or maybe

    PHP Code:
     <?php 
    if(isset($_POST['submit'])) { 

        
    $to "you@you.com"
        
    $subject =  "Form Tutorial"
        
    $name_field $_POST['name']; 
        
    $email_field $_POST['email']; 
        
    $message =  $_POST['message']; 
      
        
    $body "From: $name_field\n E-Mail: $email_field\n Message:\n  $message"
      
        if(
    mail($to$subject$body)){
             
    // If the mail was sent, then redirect
             
    header("Location:  http://www.yourpage.com");  
        }else{
           
    // Else display a message
           
    echo 'An error occured while sending your message. Try again  later';
        } 

    } else { 

        
    //Display Form here


    ?>

  5. #5
    The second one is worth

  6. #6
    thanks for handy piece of code, pretty use to read for newbie like me : ))
    http://www.kirupa.com/forum/showthread.php?t=343513live chat support software
    Last edited by rich92018; September 8th, 2010 at 12:16 PM.

  7. #7
    My form works with one exception - how do I redirect the page back to the contact form - instead of the php page?

    PHP Code:
    <?php
    //Variables.
    $user_email = $_REQUEST['email'];
    $user_name = $_REQUEST['name'];
    $user_subject = $_REQUEST['subject'];
    $user_How= $_REQUEST['How'];
    $user_comment = $_REQUEST['comment'];
    $user_send = $_REQUEST['copy'];
    $user_check = stripos("$user_email","@");
    //Body of the email to be sent.
    $body_mail = "Hello Christie, someone wants to contact you.. Details..
    Name: $user_name $user_lastname
    Email: $user_email
    subject: $user_subject
    How: $user_How
    Comment: $user_comment."
    ;
    //Body of the Email for the user requesting a copy.
    $body_email = "
    Here is a copy of the email.
    Your Name: $user_name $user_lastname.
    Your Email: $user_email.
    Your subject: $user_subject.
    Your Comment: $user_comment.
    Thank you for contacting us, we'll reply ASAP.
    Yourwebsite team."
    ;
    //Check if the user submited the data require.
    //If the @ is measing from the email address stop the user for continuing.
    if ($user_check) {
    echo
    "";
    }
    else {
    echo
    "You can't continue with out a email address, Please enter a email address.";
    exit (
    $user_check);
    }
    if (empty(
    $user_name)) {
    echo
    "<center><h2><font color='ff0000'>ERROR</font></h2></center>You didn't enter a your first name.<br>";
    exit();
    }
    elseif (!
    $user_comment) {
    echo
    "<center><h2><font color='ff0000'>ERROR</h2></center></font>Please enter a comment.";
    die();
    }
    //Everything okay? send the e-mail.
    else {
    mail('customerservice@verysweetbabycakes.com','Email from YOURWEBITE',"$body_mail","from:YOURWEBSITE");
    echo
    "your email was sent! Thank you.";
    }
    //If the checkbox if check send a copy to the user.
    if (isset($user_send)) {
    @
    mail("$user_email","The copy of your information from YOURWEBSITE","$body_email","YOURWEBSITE");
    echo
    "And a copy of the Email was send to you!";
    }
    else {
    die(
    $user_send);
    }
    ?>


    HTML Code:

    <head> <title>Contact Us</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5" /> <link rel="stylesheet" type="text/css" href="style.css" /> <style type="text/css" media="all"> form, label, input { font-size : 1em; } fieldset { width : 36em; padding : 0.5em 1em; } label { float: left; position : relative; width : 15em; display : inline; margin : .5em .5em; } label.long { width : 12em; float: left; position : relative; width : 15em; display : inline; margin : .5em .5em;} label input { display: inline; left : 100%; top : 0px; width : 14em; } label.long input { width : 18em; } input.submit { margin-left : 15em; background-color: #CCCCCC; font-weight: bold; font-size: 12px; color: #C7377D; border=1px; border-color=#cc9999;} } br { clear: both; } </style> </head> <body> <p><center> <form action="mail.php" method="post" name="mail"> <input name="redirect" value="success.html" type="hidden" /> <fieldset> <legend>Contact Form</legend> <label> Your Name:<input name="name" > </label> <label>How did you hear about us:<br /> <select name="How"> <option value="Select One....." selected="selected">Select One</option> <option value="Friend">Friend</option> <option value="Yahoo">Yahoo</option> <option value="Google">Google</option> <option value="Other Search Engine">Other Search Engine</option> <option value="Flyer/Ad">Flyer/Ad</option> <option value="Other Website">Other Website</option> </select></label> <label>Subject/Topic:<br /> <select name="subject"> <option value="Select a topic....." selected="selected">Select a topic.....</option> <option value="Product Information">Product Information</option> <option value="Customer Support">Customer Support</option> <option value="Community Involvement">Community Involvement</option> <option value="Site Technical Issue">Site Technical Issue</option> <option value="Urgent">Urgent</option> <option value="Other">Other</option> </select></label> <label class="long">Email:<input name="email" /></label><br /> <label for="comments">Comments:<textarea name="comment" cols="40" rows="8">Type Comment Here </textarea></label> <br /><input value="Submit" type="submit" class="submit" /> </fieldset> </form></center>

  8. #8
    Quote Originally Posted by korifuenc7933 View Post
    My form works with one exception - how do I redirect the page back to the contact form - instead of the php page?

    Code:
    PHP Code:
    <?php 
    //Variables. 
    $user_email = $_REQUEST['email']; 
    $user_name = $_REQUEST['name']; 
    $user_subject = $_REQUEST['subject']; 
    $user_How= $_REQUEST['How']; 
    $user_comment = $_REQUEST['comment']; 
    $user_send = $_REQUEST['copy']; 
    $user_check = stripos("$user_email","@"); 
    //Body of the email to be sent. 
    $body_mail = "Hello Christie, someone wants to contact you.. Details..  
    Name: $user_name $user_lastname  
    Email: $user_email  
    subject: $user_subject 
    How: $user_How
    Comment: $user_comment."; 
    //Body of the Email for the user requesting a copy. 
    $body_email = " 
    Here is a copy of the email. 
    Your Name: $user_name $user_lastname. 
    Your Email: $user_email.  
    Your subject: $user_subject. 
    Your Comment: $user_comment. 
    Thank you for contacting us, we'll reply ASAP. 
    Yourwebsite team."; 
    //Check if the user submited the data require. 
    //If the @ is measing from the email address stop the user for continuing. 
    if ($user_check) { 
    echo ""; 
    } 
    else { 
    echo "You can't continue with out a email address, Please enter a email address."; 
       exit ($user_check); 
    } 
    if (empty($user_name)) { 
    echo "<center><h2><font color='ff0000'>ERROR</font></h2></center>You didn't enter a your first name.<br>"; 
       exit(); 
    } 
    elseif (!$user_comment) { 
    echo "<center><h2><font color='ff0000'>ERROR</h2></center></font>Please enter a comment."; 
       die(); 
    } 
    //Everything okay? send the e-mail.      
    else { 
    mail('customerservice@verysweetbabycakes.com','Email from YOURWEBITE',"$body_mail","from:YOURWEBSITE"); 
        echo "your email was sent! Thank you."; 
    } 
    //If the checkbox if check send a copy to the user. 
    if (isset($user_send)) { 
    @mail("$user_email","The copy of your information from YOURWEBSITE","$body_email","YOURWEBSITE"); 
        echo "And a copy of the Email was send to you!"; 
    } 
    else { 
         die($user_send); 
    } 
    ?>  
    
    HTML Code:
    
    <head> <title>Contact Us</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-5" /> <link rel="stylesheet" type="text/css" href="style.css" /> <style type="text/css" media="all">  form, label, input { font-size : 1em; }  fieldset { width : 36em; padding : 0.5em 1em; }  label { float: left; position : relative; width : 15em; display : inline; margin : .5em .5em; }  label.long { width : 12em; float: left; position : relative; width : 15em; display : inline; margin : .5em .5em;}  label input { display: inline; left : 100%; top : 0px; width : 14em; }  label.long input { width : 18em; }  input.submit { margin-left : 15em; background-color: #CCCCCC; font-weight: bold; font-size: 12px; color: #C7377D; border=1px; border-color=#cc9999;}  }  br { clear: both; }  </style> </head> <body>                         <p><center>                        <form action="mail.php" method="post" name="mail">                        <input name="redirect" value="success.html" type="hidden" />                         <fieldset>                          <legend>Contact Form</legend>                         <label> Your Name:<input name="name" > </label>                       <label>How did you hear about us:<br />                         <select name="How">                        <option value="Select One....." selected="selected">Select One</option>                         <option value="Friend">Friend</option>                         <option value="Yahoo">Yahoo</option>                        <option value="Google">Google</option>                        <option value="Other Search Engine">Other Search Engine</option>                        <option value="Flyer/Ad">Flyer/Ad</option>                         <option value="Other Website">Other Website</option> </select></label>                       <label>Subject/Topic:<br />                         <select name="subject">                         <option value="Select a topic....." selected="selected">Select a topic.....</option>                        <option value="Product Information">Product Information</option>                         <option value="Customer Support">Customer Support</option>                        <option value="Community Involvement">Community Involvement</option>                        <option value="Site Technical Issue">Site Technical Issue</option>                        <option value="Urgent">Urgent</option>                         <option value="Other">Other</option> </select></label>                           <label class="long">Email:<input name="email" /></label><br />                         <label for="comments">Comments:<textarea name="comment" cols="40" rows="8">Type Comment Here </textarea></label>                         <br /><input value="Submit" type="submit" class="submit" /> </fieldset> </form></center>
    Please wrap with next time. Kthnx.

    Where is your HTML code going? Place the url, from http:// to the end of the url, where it says page-x

    PHP Code:
             header("Location:  page-x"); 
    Add that in your PHP code, just before exiting your script. That should work.

  9. #9
    Hi Forum,

    Abit of a neewbie to php, so i used this tutorial and got my contact form working, but if the user clicks submit without filling anything in, i get a whole heap of error messages, is there any way of guiding me on how to set up validation for cetain fields such as name, email etc....?

    any help much appreciated...

    Sukhi

  10. #10
    1
    posts
    Registered User
    Hey thanks for the tutorial - its works a treat thanks! I'm just starting out with php so this was exactly what I was looking for. I was wondering though once I have submitted the form the page refreshes and clears the form but there is no feedback to tell the user that the message has been received. I might be missing something but how do I achieve this?

    thanks again, Jason

  11. #11
    Can somebody please help me?

    I used the tutorial for the contact form, but somehow it's not working. When I submit the form I don't receive any message in my email. Can someone give me a suggestion?

    Here are my codes:

    <form method="POST" action="mailer.php">
    <input type="text" name="name" size="19"><br>
    <br>
    <input type="text" name="email" size="19"><br>
    <br>
    <textarea rows="9" name="message" cols="30"></textarea>
    <br>
    <br>
    <input type="submit" value="Submit" name="submit">
    </form>


    PHP:

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

    $to = "veronikacabalova@hotmail.com";
    $subject = "Form Tutorial";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];

    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

    header("Location: http://www.dui-orland-lawyer.com");
    mail($to, $subject, $body);

    } else {

    echo "blarg!";

    }
    ?>


    Thank You

  12. #12
    outstanding and so helpful code i like it

  13. #13
    Hi friends,

    Can anyone tell me will it work with Prestashop?

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012