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

PHP form result of javascript alert opening up in different window

本文关键字:窗口 表单 结果 javascript PHP      更新时间:2023-09-26

我最近为一个联系人表单做了自己的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>";
}

不要忘记在两个文件上启动会话