The reason why the form.submit of in JS could not submit the form

  • 2020-03-30 04:02:30
  • OfStack

Directly on the code put:


<div id="register">
     <h4> Registered members </h4>
     <div class="formdiv">
         <form method="post" action="register.php?action=register" name="register" id="registerForm">
             <dl>
                 <dt> Please fill in the following carefully </dt>
                 <dd class="hasspan">
                     User name: <input type="text" name="username">
                 </dd>
                 <dd class="hasspan">
                     Password: <input type="password" name="pwd">
                 </dd>
                 <dd class="hasspan">
                     Confirm password: <input type="password" name="pwdagain">
                 </dd>
                 <dd id="tximg">
                     <img src="img/face/m01.gif" alt=" Choose picture " id="faceimg">
                     <input type="hidden" name="touxiang" value="">
                     <label id="imgsrclabel">m01.gif</label>
                 </dd>
                 <dd style="margin-right:120px;">
                     Verification code: <input type="text" name="code">
                     <span>
                         <img src="code.php" alt="code" id="code" name="code">
                         <a href="#code" id="change"> In a </a>
                     </span>
                 </dd>
                 <dd class="btns">
                     <input type="button" name="submit" id="submit" value=" registered ">
                     <input type="button" name="quit" id="quit" value=" exit ">
                 </dd>
             </dl>
         </form>
     </div>
 </div>
 

The form data is submitted to this page. Below is js processing


 
 function formDeal()
 {
     var btnSubmit = document.getElementById('submit');
     var formId = document.getElementById('registerForm');
     btnSubmit.onclick = function()
     {
         //The submit() method of the form cannot submit the form
         formId.submit();
     }  }

  If the form is submitted, there is a prompt on this page

 if(!empty($_GET['action']) && $_GET['action'] == 'register')
 {
     echo ' You submitted the data ';
     exit();
 }

The result is that the test for a long time did not see the prompt message, think that the code is wrong or the method is wrong, check carefully, in the official document of the results, confirm that there is no error.

Unable to submit the formid. submit(), we temporarily had to change the type of btnSubmit to submit


 this.type="submit"

I did some research on the Internet, and the reasons come down to two:

1. There should be no label with name= "submit" in the form

2. Enctype = "multipart/form-data" cannot be missing from the form.

On test, these two points are absurd and do not solve my problem (maybe my problem environment is different).

Later, I was suggested to change the ID of the registration button to a name instead of submitting. After the correction, the form is submitted normally and the prompt message appears.

Final summary: the id of the button should not be set to submit, as this may cause confusion and cause the form's submit() method to fail to submit the form. When naming ids, it is best not to duplicate the names of existing apis to avoid unnecessary hassles.


Related articles: