英文:
php/html OnSubmit disable input type submit not give expected result
问题
以下是您要翻译的内容:
"我想学习php/html。所以我尝试着写了一个简单的代码,类似于这样:
form.php
<form autocomplete="off" action="form_result.php" enctype="multipart/form-data" method="POST">
Company Name:<br>
<input type="text" name="company" required><br>
<input type="file" name="files[]" multiple class="my-image-field"><br><br>
<input id="test" name="upload" type="submit" value="Upload" /></p>
</form>
form_result.php
<?php
if(isset($_POST['upload'])){
echo "isset $ POST upload = true";
}
else{
echo "isset $ POST upload = false";
}
?>
以上的代码会在result_form.php页面中显示预期的结果,即""isset $ POST upload = true""。
基于这个链接和这个链接中的一个答案,我修改了form.php中的代码,如下:
<form autocomplete="off" action="form_result.php" enctype="multipart/form-data" method="POST"
onsubmit="document.getElementById('test').disabled = true">
Company Name:<br>
<input type="text" name="company" required><br>
<input type="file" name="files[]" multiple class="my-image-field"><br><br>
<input id="test" name="upload" type="submit" value="Upload" /></p>
</form>
我期望的是只有"Upload"按钮变灰,而form_result.php仍然会显示与之前相同的结果,即""isset $ POST upload = true""。但结果页面显示""isset $ POST upload = false""
所以,虽然"Upload"按钮已变灰,但因为我期望结果页面显示""isset $ POST upload = true"",我错过了什么?
非常感谢您的任何回应。
提前感谢您。"
英文:
I want to learn php/html. So I play around and make a simple code something like this :
form.php
<form autocomplete="off" action="form_result.php" enctype="multipart/form-data" method="POST">
Company Name:<br>
<input type="text" name="company" required><br>
<input type="file" name="files[]" multiple class="my-image-field"><br><br>
<input id="test" name="upload" type="submit" value="Upload" /></p>
</form>
form_result.php
<?php
if(isset($_POST['upload'])){
echo "isset $ POST upload = true";
}
else{
echo "isset $ POST upload = false";
}
?>
Above code give the expected result as the page show "isset $ POST upload = true" in the result_form.php<br>
Based from one of the answer in this link and this link, I modify the code in form.php like this :
<form autocomplete="off" action="form_result.php" enctype="multipart/form-data" method="POST"
onsubmit="document.getElementById('test').disabled = true">
Company Name:<br>
<input type="text" name="company" required><br>
<input type="file" name="files[]" multiple class="my-image-field"><br><br>
<input id="test" name="upload" type="submit" value="Upload" /></p>
</form>
I'm expecting it's just the "Upload" button greyed out and the form_result.php will show the same result like before which is "isset $ POST upload = true". But it turn out the the result page show "isset $ POST upload = false" <br>
So, while the "Upload" button greyed out, but because I'm expecting that the result page show "isset $ POST upload = true", what did I miss here ?
Any kind of response would be greatly appreciated.<br>
Thank you in advanced.
答案1
得分: 2
因为disabled
的输入字段不会提交到服务器,所以submit
按钮不会包含在表单提交中。
但是,在服务器端处理表单时,你可以通过检查任何其他输入字段来实现相同的逻辑。例如:
if (isset($_POST['company'])) {
echo "isset $_POST['company'] = true";
} else {
echo "isset $_POST['company'] = false";
}
英文:
Because disabled
inputs aren't submitted to the server. So the submit
button isn't included in the form post.
Though when handling the form server-side you can achieve the same logic by just checking any other input. For example:
if (isset($_POST['company'])) {
echo "isset $ POST company = true";
} else {
echo "isset $ POST company = false";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论