0

I am building a django website,my codes are:

<script type="text/javascript"> 

$("#lxwjsubmit").click(function(){ 
    var userId=$('#id_user1').val(); 
    var password=$('#id_password1').val(); 
    var newtable2='<table  id="table2"><tr><th></th><td><input type="text" name="userId"  value="'+userId+'" /></td></tr><tr><th></th><td><input type="password" name="password"  maxlength="100"  value="'+password+'" /></td></tr></table>' 
     
    $('#table2').html(newtable2);    
    document.getElementById('form2').submit();    
    document.getElementById('form1').submit(); 
})
</script>
<div class="form-group"> 
          <form action="" method="POST" id='form1'> 
               <table> 
                  <tr><th></th><td><input type="text" name="user"   id="id_user1" /></td></tr> 
                  <tr><th></th><td><input type="password" name="password"  id="id_password1" /></td></tr> 
              </table> 

              <input type="button" value="submit"  id='lxwjsubmit'> 
          </form> 
</div> 


<div class="form-group"> 
          <form action="http://localhost/Login.do" method="POST"  id='form2' hidden='true' >         
      
              <table id="table2">
              <tr><th></th><td><input type="text" name="userId"    /></td></tr>
              <tr><th></th><td><input type="password" name="password"   /></td></tr>
              </table> 
              <input type="button" value="submit" > 
          </form> 
</div> 

I can submit each form singly. when put together,only one form can be submited. Could you correct me pls? or is there more elegant way to submit two different forms?

skystone
  • 37
  • 10

2 Answers2

0
$("#form2").ajaxSubmit({url: 'server.php', type: 'post'})
$("#id_user1").ajaxSubmit({url: 'server.php', type: 'post'})

ajax submit jQuery

Maulik
  • 2,881
  • 1
  • 22
  • 27
0

You can't submit multiple forms in one go, you can only submit one form per submit. There are some options:

  • Replace it with one big form and use <fieldset> where you now have your <form>.
  • On submitting the first form, get the values of the other form and add those via hidden fields to the first form. Takes a bit of javascript and is going to require maintenance.
  • You can use AJAX post() to submit them (combined or seperate), but this has to fit your case. Ajax could make things a bit more complex than needed.

And there are more workarounds. I recommend the first one as it keeps your code cleaner and easier to understand. Not many people expect two forms to be submitted in one go.

Martijn
  • 15,791
  • 4
  • 36
  • 68