PHP表单javascript警报在不同窗口中打开的结果

PHP表单javascript警报在不同窗口中打开的结果,php,javascript,Php,Javascript,我最近为一个联系人表单做了自己的PHP设置,我希望在用户提交表单后,PHP文件会发送一个JavaScript回音,弹出一个警告,说谢谢!,但出于某种原因,它会打开一个新窗口和一个JavaScript警报 这是我的PHP文件中的代码: <?php $firstname = $_POST['firstName']; $lastname = $_POST['lastName']; $email = $_POST['email']; $tel1 = $_POST['tel1']; $tel2 =

我最近为一个联系人表单做了自己的PHP设置,我希望在用户提交表单后,PHP文件会发送一个JavaScript回音,弹出一个警告,说谢谢!,但出于某种原因,它会打开一个新窗口和一个JavaScript警报

这是我的PHP文件中的代码:

<?php
$firstname = $_POST['firstName'];
$lastname = $_POST['lastName'];
$email = $_POST['email'];
$tel1 = $_POST['tel1'];
$tel2 = $_POST['tel2'];
$tel3 = $_POST['tel3'];
$reason = $_POST['reason'];
$message = $_POST['text'];
$formcontent="From: $firstname $lastname \n Email address: $email \n Telephone Number: $tel1 $tel2 $tel3 \n Reason: $reason \n Message: $message";
$recipient = "ilanbiala@ilanshomekitchen.x10.mx";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<script>alert('Thank you!');</script>";
?>

您正在向mail.php提交表单,因此mail.php将处理html请求并打印页面。您提交的页面是
alert('Thank you!')

您似乎不想打开新页面,因此可以对mail.php页面进行ajax调用,并在表单成功提交后调用警报

jQuery有一个或一个

你的HTML就是问题所在

<form id="form" action="mail.php" method="post">
至少,您可以将mail.php代码放在联系人页面的顶部,并添加检查以查看是否提交了表单(
if($\u SERVER['REQUEST\u METHOD']='POST'){}
),您需要将contact.html更新为contact.php,然后将表单重定向到'contact.php'而不是'mail.php'。然后邮件将加载到同一页面上,警报将出现,内容也将出现,可能需要一些调整,但您可以将其正确

祝你好运

$( function() { // $(document).ready( function() { }); this function prepares itself to be attentive to listen an event when the DOM is ready
$("#form").on("submit", function(e) { // As the form is submitted, the function is executed
e.preventDefault(); // this prevents the form to be submitted as default behaviour!
var s = $(this).serialize();  // makes a string which has all form's data in this form e.g. ?id=1&name=Heart
$.ajax({ // create an jQuery ajax call
     url : 'mail.php'+s, // this will transfer data to the mail.php. In mail.php, use $_GET with all variables
     cache : false, // changing browser cache
     success : function(data) { // success means when the ajax completed transfer of variables and that function with data as an argument is callback variable which stores all the information echoed in php file
          alert(data); // now you can manipulate that variable, i use alert function
     },
     error : function() { // this function checks if error occurs in the ajax transfer, if yes, then execute a certain function!
          alert("Form Not Submitted"); // I have just alerted not submitted!
     };
 });
 });

您还必须包含jQuery的最新版本,该版本可以找到

您可以使用以下代码: 在Mail.php中

$_SESSION['mail_msg']="Thank You";
header("Location: contact.php");
将contact.html另存为contact.php并添加以下代码

if(isset($_SESSION['mail_msg'])){
echo "<script type='text/javascript'>alert("{$_SESSION['mail_msg']}")</script>";
}
if(isset($\会话['mail\u msg'])){
回显“警报({$\u会话['mail\u msg']}”);
}

别忘了在这两个文件上启动会话。

你的HTML代码在哪里?HTML代码不是问题所在,因为我正在尝试让php中倒数第二行不打开新窗口并弹出一个弹出窗口,而只是在我的问题中的链接处弹出当前表单。听起来你想要一个ajax表单。之后我将其更改为jQuery我注意到它在你的资料里它在同一个窗口提醒我!如果您不希望页面被重定向,那么您必须使用ajax()函数,而您没有!请稍等,为您制作教程;)你能解释一下代码吗?我对jQuery语言不是很熟悉。jQuery是一个库。Javascript是一种语言。它基本上接受submitButton类,然后对mail.php页面执行一个AJAX get函数,然后根据检索到的数据执行一个函数。你可能很容易在网上找到一两本教程。我解释得很好。我不打算一步一步地介绍它,因为您可以阅读jQuery并很容易地了解到底发生了什么。不要期望用户为你做所有的工作。我们在这里为您指出了正确的方向,并在此过程中修复了所有错误。就性能而言,AJAX看起来更流畅,但它们在性能方面是相同的。每个人都会打电话到新的一页,所以这真的取决于你。AJAX需要更多的工作,但肯定可以增加用户体验。否则,我可以解释多一点!你忘了阻止表单提交。嗯@llan Biala我添加了太多的评论来帮助你,但你必须努力理解它们!如果你说我可以用JAVASCRIPT修改上面的代码,但是太大了。兄弟,你必须从某个地方开始,否则你不能依赖别人!
if(isset($_SESSION['mail_msg'])){
echo "<script type='text/javascript'>alert("{$_SESSION['mail_msg']}")</script>";
}