Search code examples
phphtmlform-submit

php/html OnSubmit disable input type submit not give expected result


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
enter image description here

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"

enter image description here

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.
Thank you in advanced.


Solution

  • 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";
    }