在php表单提交上创建成功消息

时间:2013-09-18 20:55:02

标签: php html forms email

我正在尝试让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");
    }
  }
?>

2 个答案:

答案 0 :(得分:4)

尝试此代码...您需要暂停脚本一段时间,以便用户可以看到成功或失败消息。没有睡眠,用户将无法看到消息,因为标头将立即执行。

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";
     }
 }

答案 1 :(得分:0)

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);

if(isset($_POST['contactrgtr']))
{
$query = "insert into contact_us(name,email,message)values('$name','$email','$message')";
$res = mysql_query($query);
if($res){
     $message = 'Your Message Successfully Sent';
     include_once('contact.php'); } ` 

然后在contact.php页面中,您必须定义<?php $message ?>