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
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Php form and redirect...

    Hi. A few days ago I asked about my php contact form.
    I have now simplified it and it works... Until I try to redirect after submitting.
    It redirects and seems fine, but I never receive the email....
    So this works:

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

    $to "cathrine.rydning@gmail.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!";

    }
    ?>
    But not this:

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    'Thank you for contacting me. You will now be redirected to the homepage.';

    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";
     

    }
    ?>

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by CatRyd View Post
    Hi. A few days ago I asked about my php contact form.
    I have now simplified it and it works... Until I try to redirect after submitting.
    It redirects and seems fine, but I never receive the email....
    So this works:

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

    $to "cathrine.rydning@gmail.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!";

    }
    ?>
    But not this:

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    'Thank you for contacting me. You will now be redirected to the homepage.';

    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";
     

    }
    ?>
    You don't have your mail() function in the second version.

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    "<p>Thank you for contacting me. You will now be redirected to the homepage.</p>";

    if(isset(
    $_POST['submit'])) {
    $to "you@you.com";
    $subject "Form Tutorial";
    $name_field $_POST['name'];
    $email_field $_POST['email'];
    $message $_POST['message'];
    $headers 'From: webmaster@example.com'

    $body "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
     
    echo 
    "<p>Data has been submitted to $to!</p>";
    mail($to$subject$body$headers);


    else {

    echo 
    "blarg!";

    }

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HDRebel88 View Post
    You don't have your mail() function in the second version.

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    "<p>Thank you for contacting me. You will now be redirected to the homepage.</p>";

    if(isset(
    $_POST['submit'])) {
    $to "you@you.com";
    $subject "Form Tutorial";
    $name_field $_POST['name'];
    $email_field $_POST['email'];
    $message $_POST['message'];
    $headers 'From: webmaster@example.com'

    $body "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
     
    echo 
    "<p>Data has been submitted to $to!</p>";
    mail($to$subject$body$headers);


    else {

    echo 
    "blarg!";

    }
    Of course I missed that out, silly me!
    Now it looks like this:

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    'Thank you for contacting me. You will now be redirected to the homepage.';

    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";
    mail($to$subject$body$headers); 

    }
    ?>
    and still doesn't work!!! am I being a moron and missing out something else that is as obvious as the mail function?

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by CatRyd View Post
    Of course I missed that out, silly me!
    Now it looks like this:

    PHP Code:
    <?php
    header
    ("refresh:5;url=http://needmathshelp.co.uk");
    echo 
    'Thank you for contacting me. You will now be redirected to the homepage.';

    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";
    mail($to$subject$body$headers); 

    }
    ?>
    and still doesn't work!!! am I being a moron and missing out something else that is as obvious as the mail function?
    If you don't want the $headers variable remove it from the mail function, but the emails you send to people will essentially come from no one, if you don't have the From: details set in the email header; and they may bounce or be sent to the persons spam box.

    You are setting the $to variable to the persons email address that should be getting the email correct?

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    can you post your mail() function please

  • #6
    Regular Coder jdswebservice's Avatar
    Join Date
    Aug 2010
    Location
    Moon Township, PA
    Posts
    107
    Thanks
    0
    Thanked 5 Times in 5 Posts
    you have the header() function at the first line. pseudo code below.

    PHP Code:
    <?php
    $to 
    "something";
    $headers "something";
    //etc. etc.

    if(mail($to$subject$body$headers) == TRUE) {
    header("redirect page.php");
    } else {
    $ERROR "YOU DIMWIT!";
    }

    ?>
    as seen here: http://php.net/manual/en/function.mail.php

    Mail function returns TRUE or FALSE. so check if the mail functions returns true, if it does redirect, if not, echo an error.
    Jonathan R.
    Email: jonathan@logicaldevelopment.net
    Skype, Aim, & Jabber: logicaldevelopment
    Website: http://logicaldevelopment.net/

    NOTE: When contacting me please have the following things ready:

    Thread ID (This can be found in the URL of your thread)
    Budget of Project
    Methods of contacting you/your organization
    Preferred method if many methods are provided

    Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by jdswebservice View Post
    you have the header() function at the first line. pseudo code below.

    PHP Code:
    <?php
    $to 
    "something";
    $headers "something";
    //etc. etc.

    if(mail($to$subject$body$headers) == TRUE) {
    header("redirect page.php");
    } else {
    $ERROR "YOU DIMWIT!";
    }

    ?>
    as seen here: http://php.net/manual/en/function.mail.php

    Mail function returns TRUE or FALSE. so check if the mail functions returns true, if it does redirect, if not, echo an error.
    Can't use the header function after displaying something and the OP wants to echo out a success statement prior to the redirect. I guess you could move the echo down to that if statement.

    Also you shouldn't really be checking $_POST['submit']... IE has an error submitting the data from a submit button. Add a hidden form field, and check for that during error checking.
    Last edited by HDRebel88; 07-19-2012 at 02:53 AM.

  • #8
    Regular Coder
    Join Date
    May 2011
    Posts
    124
    Thanks
    12
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by William398 View Post
    can you post your mail() function please
    This is the second time somebody has mentioned this... and I'm wonder why? The mail function is built into PHP. And what's with all the hidden images in your post?


  •  

    Posting Permissions

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