1

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.

karma
  • 1,999
  • 1
  • 10
  • 14
  • @David, thank you for the comment. My goal is actually to prevent user click the "Upload" button multiple times (because maybe he thought the form is not submitting so he click itu again and again, while actually it's submitted but need a while to process the image upload). – karma May 25 '23 at 13:27
  • 1
    If the form is submitted, your page reloads. And after being reloaded, the form is not in the submitted state – Nico Haase May 25 '23 at 13:28
  • 1
    You may use `var_dump($_POST);` in the PHP script to see what post data are actually transferred from the form (did you see disabled field(s) ?) – Ken Lee May 25 '23 at 13:30
  • NicoHaase and KenLee, thank you for the comment. I finally understand what I did wrong. Thank you for your guidance. – karma May 25 '23 at 13:38
  • @karma, the path you are going down is "if this variable exists then I'm in a POST". There's actually another way to test for that, without looking for the existence of specific form fields: https://stackoverflow.com/a/409453/231316 – Chris Haas May 25 '23 at 15:24

1 Answers1

2

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";
}
David
  • 208,112
  • 36
  • 198
  • 279
  • Thank you very much for your answer. I should've known that I can refer to "company" in form_result.php rather than refering it to "upload". – karma May 25 '23 at 13:36