php 提交表单时显示警告框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17176986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:25:49  来源:igfitidea点击:

Display alert box upon form submission

phphtmlformsalert

提问by AlwaysANovice

So I have these two pages: pageOne.phpand pageTwo.php.The form is in pageOne.php:

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

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

and doing all the data collection-validation-insertion and sending out mails in pageTwo.php(the reason i'm doing everything in two separate pages is to avoid the data re-submission upon page refresh...this was the easiest way for me to handle the issue). So far everything is working perfectly.

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

Now, I want to display a success/failure message using alert box after the form submission and tried few things w/o any luck. E.g. when I tried THISsolution on pageTwo.php, no pop up box shows up and I think that's because I have this headeron top of that page

现在,我想在提交表单后使用警告框显示成功/失败消息,并尝试了一些没有运气的事情。例如,当我在 上尝试解决方案时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>";
?>

And when tried this second solutionin pageOne.php, I get the alert box popping up every time i refresh the page and get the failure message even though the data had been inserted into database and mails were sent out. pageOne.php:

当在 中尝试第二个解决方案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:

并在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
}    ?>

Why isn't this simple thing working :( ? is there any easy way to fix it? Thank you!!

为什么这个简单的事情不起作用:(?有什么简单的方法可以解决吗?谢谢!!

UPDATE

更新

So I have made some changes according to drrcknlsn's sugession and this is what I have so far....pageOne.php:

所以我根据drrcknlsn的 sugession做了一些改变,这就是我到目前为止所做的...... 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:

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;
} ?>

With these changes now the page's redirection and success message is working, but i get the failure msg every time i open/refresh the page (i know that's because the session key is not set yet)...how can i avoid that?Thanks again!!

通过这些更改,现在页面的重定向和成功消息正在运行,但是每次打开/刷新页面时我都会收到失败消息(我知道这是因为尚未设置会话密钥)......我该如何避免这种情况?再次感谢!!

采纳答案by FtDRbwLXw6

First, a couple points:

首先,有几点:

  1. Variables (even globals) are not shared across requestslike you're trying to do in your bottom example. In order for $postedto be accessible in both pages, you must persist it in some way. Usually this involves setting a session variable (e.g. $_SESSION['posted'] = true;), but it could also be persisted in a cookie, in a database, on the filesystem, in a cache, etc.

  2. Use something like if ($_SERVER['REQUEST_METHOD'] === 'POST')instead of if ($_POST).While the latter is probably safe in most cases, it's better to get in the habit of using the former because there exists an edge case where $_POSTcan be empty with a valid POSTrequest, and it may be a hard bug to track down.

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

  2. 使用类似的东西if ($_SERVER['REQUEST_METHOD'] === 'POST')而不是if ($_POST). 虽然后者在大多数情况下可能是安全的,但最好养成使用前者的习惯,因为存在一个边缘情况,其中$_POST有效POST请求可能为空,并且可能很难追踪到错误。

One potential pattern to solve your problem using the above advice:

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

pageOne.php:

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:

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)

回答by Dawson

It looks like it's a scope problem. use:

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

global $posted = true;

http://php.net/manual/en/language.variables.scope.php

http://php.net/manual/en/language.variables.scope.php