php在同一页面中发送邮件并显示成功消息

时间:2013-11-16 13:01:50

标签: php jquery html

我在html中有一个小表格,如下所示:

 <form id="contact" action="contact.php" method="post"> 
  <table class="contact" width="400" border="0" cellspacing="2" cellpadding="0"> 
    <tr>
      <td >Your name:</td> 
      <td ><input name="name" type="text" id="name" size="32"></td> 
    </tr>
    <tr> 
      <td>Email address:</td> 
      <td><input name="email" type="text" id="email" size="32"></td> 
    </tr>
    <tr> 
      <td>Comment:</td> 
      <td>
        <textarea name="comment" cols="45" rows="6" id="comment" ></textarea>
      </td> 
    </tr>
    <tr>
      <td colspan="2" style="text-align:center;">
        <input type="submit"  value="Submit" style="padding: 3px 22px;" />
      </td>
    </tr>
  </table> 
</form>

在contact.php文件中我有这样的代码:

    if (isset($_POST['submit'])) 
    { 
      $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
     }
  ?>

现在这个工作正常。但是当用户点击提交按钮时,它只会重定向到contact.php文件。我希望当用户点击按钮时,它会以灯箱的形式在同一个html文件中显示成功消息,同时也会发送消息。有人能告诉我怎么做吗?任何帮助和建议都会非常明显。感谢

更新

我有两个不同的文件。表单位于“contact.html”内,表单操作为“contact.php”,我想在“contact.html”页面中显示“成功”和“失败”消息,而不是“contact.php”。< / p>

2 个答案:

答案 0 :(得分:3)

contact.php

上尝试类似的操作
<?php
if (isset($_POST['submit'])) { 
      $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
      {
      echo "<script>alert('Mail was sent !');</script>";
      echo "<script>document.location.href='contact.php'</script>";
      }
      else
      {
      echo "<script>alert('Mail was not sent. Please try again later');</script>";
      }
     }

答案 1 :(得分:0)

试试这个

<html>
<head>
</head>
<body>
 <form id="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
  <table class="contact" width="400" border="0" cellspacing="2" cellpadding="0"> 
    <tr>
      <td >Your name:</td> 
      <td ><input name="name" type="text" id="name" size="32"></td> 
    </tr>
    <tr> 
      <td>Email address:</td> 
      <td><input name="email" type="text" id="email" size="32"></td> 
    </tr>
    <tr> 
      <td>Comment:</td> 
      <td>
        <textarea name="comment" cols="45" rows="6" id="comment" ></textarea>
      </td> 
    </tr>
    <tr>
      <td colspan="2" style="text-align:center;">
        <input type="submit"  value="Submit" style="padding: 3px 22px;" />
      </td>
    </tr>
  </table> 
</form>
<?php
   if(isset($_POST))
   {
       $name = $_POST['name'];
      $email = $_POST['email'];
      $ToEmail = 'test@test.com';
      $EmailSubject = 'Site contact form '; 
      $mailheader = "From: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
      $MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
      $MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
      $MESSAGE_BODY .= "Subject:".$_POST['subject']."<br />";  
      $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>"; 
      if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
       {
                echo "success ";
       }
     else
     {
       echo "Failure";
     }
   }


 ?>
</body>
</html>