我试图使php邮件表单在成功提交时显示成功消息。我是php的新手,我不知道如何使其工作。就像它现在的样子,提交后,站点只是在页面顶部重新加载。我希望它刷新在联系人div并显示一个成功消息。
联系div HTML:
<div class="contact-wrapper" >
<div class="contact">
<div class="contact-left" id="contact">
<?php
if (isset($_REQUEST['email'])) {
echo "Thank you for your message.";
}
$mail_form = include('php/mail_form.php'); ?>
</div> <!-- end div contact -->
PHP的联系人:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("idc615@gmail.com", "Subject: $subject",
$message, "From: $email" );
header("location: index.php");
}
}
?>
请尝试此代码...您需要暂停脚本一段时间,以便用户可以看到成功或失败消息。没有睡眠,用户将无法看到消息,因为头将立即执行。
if(mail("idc615@gmail.com", "Subject: $subject",$message, "From: $email" )){
echo "Thank you for your message";
sleep(2); // sleep for 2 seconds. Enough to display success message
//header will execute after 2000ms(2s)
header("location: index.php");
}else{
echo "Unable to send message";
sleep(2); // sleep for 2 seconds
header("location: index.php");
}
或尝试下面给出的代码:
从PHP联系
if(mail("idc615@gmail.com", "Subject: $subject",$message, "From: $email" )){
header("location: index.php?status=1");
}else{
header("location: index.php?status=0");
}
在index.php中
if(isset($_GET['status'])){
$status = $_GET['status'];
if($status == 1){
echo "Thank you for your message";
}else if($status == 0){
echo "Unable to send message";
}
}