我有一个像这样的 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”。
最佳答案
在您的 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>";
}
}
关于php发送邮件并在同一页面显示成功信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018775/