提交表单后显示警告框 [英] Display alert box upon form submission

查看:129
本文介绍了提交表单后显示警告框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这两个页面: pageOne.php pageTwo.php 。表单在 pageOne.php

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

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

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

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.

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

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 THIS solution on pageTwo.php, no pop up box shows up and I think that's because I have this header on top of that page

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

And when tried this second solution in 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:

<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>

并在 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!!

更新

所以我根据 drrcknlsn 的建议,这就是我到目前为止的.... pageOne.php

So I have made some changes according to drrcknlsn's sugession and this is what I have so far....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>

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!!

推荐答案

首先,几点:


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

  1. Variables (even globals) are not shared across requests like 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.

使用类似 if($ _SERVER ['REQUEST_METHOD'] === 'POST')而不是 if($ _POST)尽管后者在大多数情况下可能是安全的,但最好获得习惯使用前者,因为存在一种极端情况,其中 $ _ POST 可以通过有效的 POST 请求为空,这可能是很难追踪的错误。

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 $_POST can be empty with a valid POST request, and it may be a hard bug to track down.

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

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

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)

这篇关于提交表单后显示警告框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆