为什么当页面刷新时,表单会自动重新提交 [英] Why a form would automatically resubmitted when the page is refreshed

查看:403
本文介绍了为什么当页面刷新时,表单会自动重新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我设置一个javascript cookie来测试是否刷新页面 - if它是'是'然后做它等等...这是对身体负荷事件的行动。通过隐藏的iFrame提交表单。



问题是,当发生这种情况时,页面上的购买按钮(提交)表单将被重新提交并返回到页面它只是从(篮子)返回。这发生在FF但不是在IE浏览器。



任何帮助表示赞赏。

解决方案

解决这个问题的一种常见方法是使用POST收件人:



在捕获脚本完成POST所需的所有处理之后,抛出一个位置:标题重定向到当前请求的URI。这会影响浏览器将最终目标识别为未收到POST,因此刷新不会重新提交POST。



PHP示例



page.php



 <?php 
if(!empty( $ _POST)){
//做一些事情
header('Location:page.php');
//第二次_POST不会有任何数据,
//所以不用担心无限循环
exit;
}
?>
< h1>我永远不会尝试重新提交POST数据!< / h1>


Does anyone know why a form would automatically resubmitted when the page is refreshed?

I an setting a javascript cookie to test whether or not to refresh the page - if it's 'YES' then do it etc... This is action on the body load event. The form submits through a hidden iFrame.

The trouble is, when this occurs the 'buy' button (submit) form on the page is resubmitted and thus returns to the page it's just returned from (the basket). This happens in FF but not in IE.

Any help appreciated.

解决方案

A common approach to address this issue is to monkey with the POST recipient:

After the catching script has done all of the processing required by the POST, throw a Location: header that redirects to the the currently requested URI. This has the affect of the browser recognizing the final destination as not having received a POST, and therefore refreshing won't resubmit the POST.

PHP Example

page.php

<?php
if (!empty($_POST)) {
    // Do some stuff
    header('Location: page.php');
    // The second time around _POST won't have any data,
    // so there's not worry of an infinite loop
    exit;
}
?>
<h1>Hi, I will never try to resubmit POST data!</h1>

这篇关于为什么当页面刷新时,表单会自动重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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