PHP表单结果的javascript警报打开在不同的窗口
PHP form result of javascript alert opening up in different window
我最近为一个联系人表单做了自己的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>";
?>
echo命令的最后一行是我插入脚本的地方,在成功提交时用弹出窗口提醒用户。如果你有一个更好的主意,那将是伟大的,但我仍然想知道的原因,我的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提交表单,因此mail.php将处理html请求并打印页面。您正在提交的页面是<script>alert('Thank you!');</script>
似乎你不想打开一个新的页面,所以你可以对mail.php页面进行ajax调用,一旦表单被成功提交,你可以调用一个警报。
jQuery有一个ajax函数或者post函数HTML 这个问题。
<form id="form" action="mail.php" method="post">
这是在'contact.html'上,所以当你提交表单时,它会加载'mail.php'。
您需要一个AJAX表单,或者至少在同一个页面上加载表单。使用jQuery,您可以执行如下操作:
$('.submitButton').click(function(){
$.get('mail.php',$("#form").serialize(), function(data){
alert(data);
});
});
至少,你可以把mail.php代码放在联系人页面的顶部,并添加一个检查来查看是否提交了表单(if($_SERVER['REQUEST_METHOD']=='POST'){}
)。你需要更新contact.html到contact.php,然后将表单重定向到'contact.php'而不是'mail.php'。然后邮件会在同一页面上加载,警报会出现,内容也会出现,可能需要一些调整,但你可以得到正确的
好运。
可以使用以下代码:在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>";
}
不要忘记在两个文件上启动会话
- Javascript更新孙窗口中的表单元素
- javascript弹出窗口没有正确加载html表单
- 窗口提醒或类似内容中的电子邮件表单
- 在jQuery中的IFrame中提交表单后刷新父窗口
- 仅当表单有效时关闭弹出窗口
- 使用Javascript在弹出窗口中提交表单
- 表单按钮在同一窗口中打开新的URL
- 将Overlay添加到弹出窗口并添加输入类型字段以退出表单
- 选项卡在新窗口上提交表单,原因
- 提交表单的可能性(左-/中-/在新选项卡或窗口中打开-)单击html链接
- Javascript在新窗口中重新提交具有特定维度的表单
- 提交表单时在弹出窗口中打开响应
- 提交表单后,模式窗口未关闭
- 为什么这个JS确认窗口没有在表单提交时启动
- 弹出窗口中的表单验证
- 如何在新窗口中提交表单,然后重定向父页面
- 提交在弹出窗口中创建的表单
- Javascript表单提交打开新窗口选项卡,然后重定向父页面
- j查询弹出窗口 在发布方法中提交表单
- 弹出窗口和 PHP 表单