如何为 php 表单使用重置按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/711324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use a reset button for php form?
提问by lynn
I've got a form that I'm handling with PHP. Before the user submits it, the Reset button works. But when they submit and the page reloads (I've made the form fields sticky based on the $_POST values) the reset doesn't work. How can I fix that? EDIT: For example, a checkbox on the form:
我有一个我正在用 PHP 处理的表单。在用户提交之前,重置按钮起作用。但是,当他们提交并重新加载页面时(我根据 $_POST 值使表单字段具有粘性),重置不起作用。我该如何解决?编辑:例如,表单上的复选框:
<input type="checkbox" <?php if (isset($_POST['cb12'])){echo 'checked="checked"';} ?> name="cb12" tabindex="34" id=cb value="Education">
And the HTML:
和 HTML:
<tr>
<td colspan="5" valign="top" class="addit" ><div class="sectionbreak topsp" >
<input type="hidden" name="failure" value="failure.html" >
<p>
<input type="submit" name="Submit" value="Submit" tabindex="49">
Sends your application by email to The Boggs</p>
<p>
<input type="reset" name="Reset" value="Reset" tabindex="50">
Clears all the fields</p>
</div></td>
</tr>
EDIT: In the end, I just hid the button if the form has been submitted (but isn't complete). Maybe no one will notice.
编辑:最后,如果表单已提交(但不完整),我只是隐藏了按钮。也许没有人会注意到。
回答by Adi
The reset button undoes changes to the form, made by the user, it doesn't erase default values. If you want to erase all default values, you could use JavaScript to do something like:
重置按钮会撤消用户对表单所做的更改,但不会删除默认值。如果要删除所有默认值,可以使用 JavaScript 执行以下操作:
<script type="text/javascript">
function resetForm(myFormId)
{
var myForm = document.getElementById(myFormId);
for (var i = 0; i < myForm.elements.length; i++)
{
if ('submit' != myForm.elements[i].type && 'reset' != myForm.elements[i].type)
{
myForm.elements[i].checked = false;
myForm.elements[i].value = '';
myForm.elements[i].selectedIndex = 0;
}
}
}
</script>
...
...
<form id="myFormId" ...>
<input name="reset" type="reset" onclick="resetForm('myFormId'); return false;" />
...
...
回答by markus
You could react to the reset event with an unset($_POST).
您可以使用unset($_POST).
回答by Sukanya Suku
The reset button undoes changes to the edited form values, made by the user, it doesn't erase default values.The reset button commonly used in edited pages or submit pages
重置按钮撤消用户对已编辑表单值所做的更改,但不会删除默认值。重置按钮通常用于编辑页面或提交页面
<input type="reset" value="Reset" name="reset" />
回答by da5id
I've just been through this exact thing, see my previous question & the incredible helpful answers.
我刚刚经历了这件事,请参阅我之前的问题和令人难以置信的有用答案。
In the end I had to do a manual reset of the values in PHP.
最后,我不得不手动重置 PHP 中的值。
EDIT: Not quite the same scenario for you as you seem to be populating the form values based on $_POST rather than $_SESSION as I did. In which case, see the answer I accepted via the link above.
编辑:对您来说情况不太一样,因为您似乎像我一样基于 $_POST 而不是 $_SESSION 填充表单值。在这种情况下,请参阅我通过上面的链接接受的答案。
回答by Karel
Answered this already in another post:
已经在另一篇文章中回答了这个问题:
I'm just an intermediate in PHP, and a bit lazy to dive into a new language like JQuery, but isn't the following a simple and elegant solution?
我只是一个 PHP 中级,有点懒于深入研究像 JQuery 这样的新语言,但以下不是一个简单而优雅的解决方案吗?
<input name="Submit1" type="submit" value="Get free quote" />
<input name="submitreset" type="submit" value="Reset" />
Can't see a reason why not have two submit buttons, just with different purposes. Then simply:
看不出为什么没有两个提交按钮的原因,只是目的不同。然后简单地:
if ($_POST['submitreset']=="Reset") {
$_source = "--Choose language from--";
$_target = "--Choose language to--"; }
You just redefine your values back to whatever the default is supposed to be.
您只需将值重新定义为默认值即可。

