Why a form would automatically resubmitted when the page is refreshed

半世苍凉 提交于 2019-12-12 04:18:11

问题


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.


回答1:


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>



回答2:


FireFox is designed to save your forms/inputs, which includes submit buttons. This is really helpful when developing sites with forms so you don't have to keep filling them out, but it can get in the way sometimes, but it's a browser feature, not a code issue.




回答3:


If you POSTed to get to the current page (instead of a post then a response.redirect to GET the current page), when you hit the browser's refresh button, it's going to re-POST. I'm not sure how that relates to your hidden Iframe issue, though. (It seems I'm duplicating what Shad just said, in essence).



来源:https://stackoverflow.com/questions/9220942/why-a-form-would-automatically-resubmitted-when-the-page-is-refreshed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!