在表单提交时显示警告框


Display alert box upon form submission

我有这两页:pageOne.phppageTwo.php。表格在pageOne.php:

<form method="post" action="pageTwo.php"> .... </form>

并在pageTwo.php中执行所有数据收集-验证-插入和发送邮件(我在两个单独的页面中执行所有操作的原因是为了避免在页面刷新时重新提交数据…这是我处理这个问题最简单的方法)。到目前为止,一切都很顺利。

现在,我想在表单提交后使用警告框显示成功/失败消息,并尝试了一些事情,但没有任何运气。例如,当我在pageTwo.php上尝试这个解决方案时,没有弹出框显示,我认为这是因为我在该页面的顶部有这个header

<?php header("Location: http://TestPages.com/pageOne.php");  ?>
<?php
if( $_POST ) {
     //collect the data
     //insert the data into DB
     //send out the mails IFF the data insertion works
     echo "<script type='text/javascript'>alert('It worked!')</script>";
}else
     echo "<script type='text/javascript'>alert('Did NOT work')</script>";
?>

当在pageOne.php中尝试第二种解决方案时,我每次刷新页面时都会弹出警告框并获得失败消息,即使数据已插入数据库并发送邮件。pageOne.php:

<html>
<body>
   <?php
   if( $GLOBALS["posted"])  //if($posted) 
      echo "<script type='text/javascript'>alert('It worked!')</script>";
   else
      echo "<script type='text/javascript'>alert('Did NOT work')</script>";
   ?>
   <form method="post" action="pageTwo.php"> .... </form>
</body>

and in pageTwo.php:

<?php header("Location: http://TestPages.com/pageOne.php");  ?>
<?php
$posted = false;
if( $_POST ) {
     $posted = true;
     //collect the data
     //insert the data into DB
     //send out the mails IFF the data insertion works
}    ?>

为什么这个简单的事情不工作:(?有什么简单的办法能修好它吗?谢谢你! !

所以我根据drrcknlsn的建议做了一些改变,这就是我到目前为止....pageOne.php:

 <?php
   session_start();
   if (isset($_SESSION['posted']) && $_SESSION['posted']) {
      unset($_SESSION['posted']);
      // the form was posted - do something here
  echo "<script type='text/javascript'>alert('It worked!')</script>";
   } else
      echo "<script type='text/javascript'>alert('Did NOT work')</script>";
 ?>
<html> <body>
   <form method="post" action="pageTwo.php"> .... </form>
</body> </html>

and pageTwo.php:

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     $_SESSION['posted'] = true;
     //collect the data
     //insert the data into DB
     //send out the mails IFF the data insertion works
     header('Location: http://TestPages.com/pageOne.php');
     exit;
} ?>

随着这些变化,现在页面的重定向和成功消息正在工作,但我每次打开/刷新页面时都会收到失败消息(我知道这是因为会话密钥尚未设置)…我怎么能避免?谢谢!

首先,说明几点:

  1. 变量(甚至全局变量)不跨请求共享,就像你在底部示例中试图做的那样。为了在两个页面中都可以访问$posted,必须以某种方式持久化它。通常这涉及到设置会话变量(例如$_SESSION['posted'] = true;),但它也可以保存在cookie、数据库、文件系统、缓存等中。

  2. 使用if ($_SERVER['REQUEST_METHOD'] === 'POST')代替if ($_POST) .虽然后者在大多数情况下可能是安全的,但最好习惯使用前者,因为存在一个边缘情况,$_POST可以与有效的POST请求一起为空,这可能是一个难以追踪的错误。

使用上述建议解决问题的一个潜在模式:

pageOne.php:

<?php
session_start();
if (isset($_SESSION['posted']) && $_SESSION['posted']) {
    unset($_SESSION['posted']);
    // the form was posted - do something here
}
?>
...
<form>...</form>

pageTwo.php:

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $_SESSION['posted'] = true;
    // do form processing stuff here
    header('Location: pageOne.php');
    exit;
}
// show an error page here (users shouldn't ever see it, unless they're snooping around)

看起来这是一个范围问题。用途:

global $posted = true;
http://php.net/manual/en/language.variables.scope.php