停止重新发布张贴的表单 [英] Stop a posted form from re-posting

查看:95
本文介绍了停止重新发布张贴的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在提交数据后停止发布网页。例如,我可以使用下面的表单:

 < form action ='#'method ='post'> 
< input type ='text'name ='name'/>
< input type ='submit'name ='submit'/>
< / form>

然后像这样发帖:

 <?php 
if(isset($ _ POST ['submit'])){
echoHello。 $ _ POST [名];
}
?>

但是当页面刷新时,它会一次又一次地执行相同的操作,直到用户离开页面或者在页面上进行全面刷新。这是一个巨大的问题,因为它是必要的,我可以阻止一旦完成后进行预制。



我尝试过使用元标记(< meta http-equiv =refreshcontent =5> )来刷新页面,但这会造成一个问题, ($ _ POST ['submit']);< / code>和 unset($ _ POST),我试图取消设置post变量; ),但这不起作用,我不能使用header(Location:...),因为我使用的是WordPress,因此标题总是由系统发送。
$ b

所以我想问一下我们的社区:我如何停止重新提交数据?





我现在在做什么:

 <?php 
if(!isset($ _ SESSION)){
session_start();
}
$ token = rand();
if($ _POST){
if(isset($ _ SERVER ['token'])){
if($ _ POST ['token'] == $ _SESSION ['token'] ){
$ continue = FALSE;
}
else {
$ continue = TRUE;
}
}
else {
$ _SERVER ['token'] = $ _POST ['token'];
$ continue = TRUE;
}

if($ continue === TRUE){
if(isset($ _ POST ['update'])){
//更新代码
}
if(isset($ _ POST ['win'])){
//赢得代码
}
if(isset($ _ POST ['credit '])){
//做信用代码
}
}
}

表格:

 < form method =postaction =#> 
< select name =select>
< option value =false< if($ results-> data [$ i] ['marked'] ==false){echoselected; }?>>没有注意< / option>
< option value =true<? if($ results-> data [$ i] ['marked'] ==true){echoselected; }?>>需要注意< / option>
< / select>
< input type =hiddenname =tokenvalue =<?php echo $ token;?> />

< input type =hiddenname =betidvalue =< ;? echo $ results-> data [$ i] ['bet_id'];?> />
< input name =updateid =class =btn btn-defaultvalue =Updatetype =submit>
< / form>


解决方案

您可以这样做:

 <?php 
$ token = rand(); //或者你是否想要生成一个随机令牌
?>

< form action ='#'method ='post'>
< input type ='text'name ='name'/>
< input type =hiddenname =tokenvalue =<?php echo $ token;?> />
< input type ='submit'name ='submit'/>
< / form>

然后抓住帖子

if(isset($ _ SESSION ['token'])){$ b $ < b if($ _ POST ['token'] == $ _SESSION ['token']){
$ continue = FALSE;
} else {
$ continue = TRUE;
}
} else {
$ continue = TRUE;
}

if($ continue === TRUE){
$ _SESSION ['token'] = $ _POST ['token'];
// DO DO SOMETHING
}
}
?>

它可以做得更小或更紧凑,但您应该明白代码背后的想法,这样如果已经提交数据的用户可以提交新数据,如果他访问表单的新数据,但是如果页面刷新数据将不会再次处理



当然你必须确定SESSION是startet

I need to be able to stop a page from posting once the data in it has been submitted. For example I could have this form:

<form action='#' method='post'>
    <input type='text' name='name' />
    <input type='submit' name='submit' />
</form>

Then catch the post like this:

<?php
if (isset($_POST['submit'])) {
    echo "Hello " .  $_POST['name'];
}
?>

But when the page refreshes it will preform the same action time and again until either the user leaves the page or does a full refresh on the page. This is causing me a huge issue as it is imperative I am able to stop the post from preforming once it has been done.

I have tried using a meta tag (<meta http-equiv="refresh" content="5">) to refresh the page, but this caused an issue as every few seconds it would do it and the page data was lost, I have tried to unset the post variable (unset($_POST['submit']); and unset($_POST);), but this did not work, I cannot use header("Location: ...") as I am using WordPress and thus headers are always sent by the system.

So I ask this of the community we have: How can I stop the post from re-submitting the data?

EDIT

What I am now doing:

<?php
if (!isset($_SESSION)) {
    session_start();
}
$token = rand();
if ($_POST) {
    if (isset($_SERVER['token'])) {
        if($_POST['token'] == $_SESSION['token']){
            $continue = FALSE;
        }
        else {
            $continue = TRUE;
        }
    }
    else {
        $_SERVER['token'] = $_POST['token'];
        $continue = TRUE;
    }

    if ($continue === TRUE) {
        if (isset($_POST['update'])) {
            //Do update code
        }
        if (isset($_POST['win'])) {
            //Do win code
        }
        if (isset($_POST['credit'])) {
            //do credit code
        }
    }
}

Form:

<form method="post" action="#">
    <select name="select">
        <option value="false" <? if ($results->data[$i]['marked'] == "false") { echo "selected"; } ?>>No Attention</option>
        <option value="true" <? if ($results->data[$i]['marked'] == "true") { echo "selected"; } ?>>Needs attention</option>
    </select>
    <input type="hidden" name="token" value="<?php echo $token; ?>" />

    <input type="hidden" name="betid" value="<? echo $results->data[$i]['bet_id']; ?>" />
    <input name="update" id="" class="btn btn-default" value="Update" type="submit">
</form>

解决方案

You can do it like this:

<?php
$token = rand(); //Or how ever you want to generate a random token
?>

<form action='#' method='post'>
    <input type='text' name='name' />
    <input type="hidden" name="token" value="<?php echo $token; ?>" />
    <input type='submit' name='submit' />
</form>

Then catching the post

<?php
if(isset($_POST['submit'])){
   if (isset($_SESSION['token']) ) {
       if($_POST['token'] == $_SESSION['token']){
           $continue = FALSE;
       }else{
           $continue = TRUE;
       }
   }else{
     $continue = TRUE;
   }

  if($continue === TRUE){
     $_SESSION['token'] = $_POST['token'];
     //DO SOMETHING
  }
}
?>

It can properly be made smaller / more compact but you should get the idea behind the code, this way if a user who has already submitted data can submit new data if he access the form a new, but if the page refreshes the data won't be procesed again

Of course you have to be sure that SESSION is startet

这篇关于停止重新发布张贴的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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