All is working fine, except redirect to the thank you page. I can send mail both sides, but after contact form is submitted I can't redirect the page to thankyou.html
Am I doing something wrong in the code?
session_start();
$serverMail = 'example@gmail.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];
$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
}
else{
//to server side
if(md5($captcha) == $img_session){
$mailMessage = "
$name
some message";
if (mail($serverMail, $toAdmin, $mailMessage, $headers)) {
}
}
// to guests side
if(md5($captcha) == $img_session){
$mailMessage = "
$name
some message";
if (mail($email, $toGuest, $mailMessage, $headers)) {
header('Location:https://stackoverflow.com/');
exit();
}
}
else{
$output = "Wrong Captcha Code!";
}
}
echo $output;
I have to if(md5($captcha) == $img_session)
because $mailMessage
are different.
There is no need to test the captcha twice.
Once the captcha is checked, build and send both emails, keeping the status in a temp variable. Once the 2 emails are sent check the status of both and code accordingly for picking up the error whereever it is found.
session_start();
$serverMail = 'example@gmail.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];
$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";
if(empty($email) or empty($name) or empty($email) or empty($message)){
$output = "All fields are required!";
} else{
if(md5($captcha) == $img_session){
//to server side
$mailMessage = "\n$name\n\nsome message";
$m1 = mail($serverMail, $toAdmin, $mailMessage, $headers)) {
// to guests side
$mailMessage = "\n$name\n\nsome other message";
$m2 = mail($email, $toGuest, $mailMessage, $headers)) {
if ( $m1 && $m2 ) {
header('Location:https://stackoverflow.com/');
exit;
} else {
// one or both emails failed to send
if( ! $m1 ) {
// error message for server mail failure
}
if ( ! $m2 ) {
// error message for guest mail failure
}
} else {
$output = "Wrong Captcha Code!";
}
}
echo $output;
因为你在第一封邮件后使用“退出”!
Try using this code, if it doesnt give try using a different browsers. it may be from the
echo '<META HTTP-EQUIV="refresh" CONTENT="0;url=http://helloworld/contact/thankyou.html">';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.