所以我在将表单提交到主页后尝试进行重定向。很好,它的工作但是有一个问题。用户按下"提交"按钮,但立即重定向到主页。我怎样才能让用户第一次收到#34;谢谢你"消息和被重定向到主页?
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon@template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
header('Location: https://saprs.000webhostapp.com/index.html');
?>
答案 0 :(得分:1)
请使用jQuery和ajax。
HTML和jQuery:
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function submitForm() {
//Do validation and submit form
$.ajax({
url: "mail.php",
type: "POST",
}).done(function( data ) {
alert(data);
if(data==1){
alert('Success');
window.location.href = 'test.php';//Your location
}
else
{
alert('failed');
}
});
return false;
}
</script>
</head>
<body>
<form method="post" id="mailData" name="mailData" onsubmit="return submitForm();">
<label>Contact Form:</label><br>
<input type="submit" value="Submit" />
</form>
<div id="output"></div>
</body>
</html>
服务器端PHP代码:
<?php
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) {
echo 1;
}
else {
echo 0;
}
?>
答案 1 :(得分:0)
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if($mail_status == true){
echo "<script>alert('Thank you for the message. We will contact you shortly.');</script>";
header('Location:https://saprs.000webhostapp.com/contact_page.html');
}else{
echo "<script>alert('Sorry! Please try again.');</script>";
header('Location:https://saprs.000webhostapp.com/contact_page.html');
}
答案 2 :(得分:0)
只需从代码中删除标题,然后替换为以下
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location.href = 'index.html';
</script>
<?php
}else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon@template-help.com');
window.location.href = 'contact_page.html';
</script>
<?php } ?>
答案 3 :(得分:0)
替换
header('Location: https://saprs.000webhostapp.com/index.html');
与
echo "<script type='text/javascript'>window.location.href='https://saprs.000webhostapp.com/index.html'</script>";