【发布时间】:2017-06-19 10:29:13
【问题描述】:
所以我在将表单提交到主页后尝试进行重定向。很好,它可以工作,但有一个问题。当用户按下“提交”按钮时,消息不会显示,但会立即重定向到主页。我怎样才能让用户首先收到“谢谢”消息,然后被重定向到主页?
<?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');
?>
【问题讨论】:
-
把谢谢你,在你重定向到的页面上
-
在回显/输出后不能使用 header()
-
请注意,我对此完全陌生,所以我可能会搞砸很多事情。