...

View Full Version : Php form and redirect...



CatRyd
07-18-2012, 01:00 AM
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
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
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";


}
?>

HDRebel88
07-18-2012, 09:15 AM
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
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
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
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!";

}

CatRyd
07-18-2012, 11:13 AM
You don't have your mail() function in the second version.



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

HDRebel88
07-18-2012, 11:57 AM
Of course I missed that out, silly me!
Now it looks like this:


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

Arcticwarrio
07-18-2012, 02:53 PM
can you post your mail() function please

jdswebservice
07-19-2012, 01:08 AM
you have the header() function at the first line. pseudo code below.


<?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.

HDRebel88
07-19-2012, 02:46 AM
you have the header() function at the first line. pseudo code below.


<?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.

HDRebel88
07-19-2012, 07:14 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum