Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading