在提交时禁用输入类型提交未产生预期结果。

huangapple go评论66阅读模式
英文:

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 :

&lt;form autocomplete=&quot;off&quot; action=&quot;form_result.php&quot; enctype=&quot;multipart/form-data&quot; method=&quot;POST&quot; 
onsubmit=&quot;document.getElementById(&#39;test&#39;).disabled = true&quot;&gt;
    Company Name:&lt;br&gt;
    &lt;input type=&quot;text&quot; name=&quot;company&quot; required&gt;&lt;br&gt;
    &lt;input type=&quot;file&quot; name=&quot;files[]&quot; multiple class=&quot;my-image-field&quot;&gt;&lt;br&gt;&lt;br&gt;
&lt;input id=&quot;test&quot; name=&quot;upload&quot; type=&quot;submit&quot; value=&quot;Upload&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;

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[&#39;company&#39;])) {
  echo &quot;isset $ POST company = true&quot;;
} else {
  echo &quot;isset $ POST company = false&quot;;
}

huangapple
  • 本文由 发表于 2023年5月25日 21:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定