只提交一次表格一次;当页面刷新时不要提交 [英] Submit a form only once; don't submit when page is refreshed

查看:150
本文介绍了只提交一次表格一次;当页面刷新时不要提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题非常基本,我几乎不好意思问这个问题,但这让我很难过。我写了一段简短的代码片段来解释它:

 < form id =MyFormmethod =postenctype =multipart / form-data> 
< input type =fileid =get_filename =get_file>
< input type =submitname =submitFormvalue =Submit>
< / form>

<?php
if(isset($ _ POST ['submitForm'])){
echo< script type ='text / javascript'> alert '已选择')LT /脚本>中;
} else {
echo< script type ='text / javascript'> alert('Not selected')< / script>;
}
?>

当我第一次加载此页面时,警报显示未选中。好。我提交表单并提示选定。也不错。但是当我刷新页面时,它仍然是选中。我不明白为什么IF声明允许这种情况发生。我希望我的表单提交数据一次,而不是每次页面刷新时都重复执行。



这似乎是noobs问他们什么时候懒得使用谷歌,但我看过(这个网站上甚至有一些线索解决了这个问题,但没有人提出过解决方案)。我已经阅读过HTML表单,但我仍然不明白。



这是每个人都知道的,但是我呢?有人能告诉我吗?谢谢。

编辑:如果有人曾经绊倒过这个线程,需要一个快速修复的问题,这里是我偶然发现的一个。只需再次按下选择键,轻松完成使用jQuery。表单被重新提交,但表单变量全部被清除。

解决方案

当您重新加载页面时,它将重新提交POST请求,这意味着 submitForm = Submit 是请求主体的一部分。



您应该使用 Post / Redirect / Get 模式以避免这种情况。


This question is so basic that I'm almost embarrassed to ask it, but it's got me stumped. I've written a short code snippet to explain it:

<form id="MyForm" method="post" enctype="multipart/form-data" >
    <input type="file" id="get_file" name="get_file" >
    <input type="submit" name="submitForm" value="Submit" >
</form>

<?php
    if (isset($_POST['submitForm'])) {
    echo "<script type='text/javascript'>alert('Selected')</script>";
} else {    
    echo "<script type='text/javascript'>alert('Not selected')</script>";
}
?>

When I load this page the first time, the alert says "Not selected". Good. I submit the form and the alert says "Selected". Also good. But when I refresh the page, it's still "Selected". I don't understand why the "IF" statement allows this to happen. I want my form to submit data once and not do it again until my user does it, not every time the page refreshes.

This seems like the kind of question that noobs ask when they're too lazy to use Google, but I've looked (there are even some threads on this site that sort of address this issue, but nobody has presented a solution). I've read up on HTML forms, but I still don't get it.

Is this something everyone knows but me? Can somebody clue me in? Thanks.

EDIT: If anybody ever stumbles over this thread and needs a quick-and-dirty fix, here's one I just stumbled on. Just press the select key again, easily done using jQuery. The form is resubmitted, but the form variables are all cleared. Not elegant, maybe, but it works perfectly.

解决方案

When you reload the page, it is resubmitting the POST request, which means the submitForm=Submit is part of the request body.

You should use the Post/Redirect/Get pattern to avoid this.

这篇关于只提交一次表格一次;当页面刷新时不要提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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