加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:51:38 所属栏目:PHP教程 来源:网络整理
导读:我在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
我在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"]."rn"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."rn"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1rn"; 
      $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”.

解决方法

在你的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"]."rn"; 
      $mailheader .= "Reply-To: ".$_POST["email"]."rn"; 
      $mailheader .= "Content-type: text/html; charset=iso-8859-1rn"; 
      $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,$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>";
      }
     }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读