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
Display alert box upon form submission
提问by AlwaysANovice
So I have these two pages: pageOne.php
and pageTwo.php
.The form is in pageOne.php
:
所以我有这两页:pageOne.php
和pageTwo.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 header
on 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:
首先,有几点:
Variables (even globals) are not shared across requestslike you're trying to do in your bottom example. In order for
$posted
to 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.Use something like
if ($_SERVER['REQUEST_METHOD'] === 'POST')
instead ofif ($_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$_POST
can be empty with a validPOST
request, and it may be a hard bug to track down.
变量(甚至全局变量)不会像您在底部示例中尝试做的那样跨请求共享。为了
$posted
在两个页面中都可以访问,您必须以某种方式持久化它。通常这涉及设置会话变量(例如$_SESSION['posted'] = true;
),但它也可以保存在 cookie、数据库、文件系统、缓存等中。使用类似的东西
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;