Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form data both on clicking enter and hitting the submit button from a Bootstrap modal window

Tags:

Here's my form that's in my modal window:

I have no close button and I have disabled the window from closing on pressing esc. I want to be able to submit the form data on pressing submit or on pressing the enter key.

<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">     <div class="modal-header">         <h3 id="myModalLabel">Enter your Credentials</h3>     </div>     <div class="modal-body">         <form id="login-form" class="form-horizontal" accept-charset="UTF-8"  data-remote="true"">             <div class="control-group">                 <label class="control-label" for="inputEmail">Email</label>                 <div class="controls">                     <input type="email" id="email" name="email" value=""  placeholder="Email">                 </div>             </div>             <div class="control-group">                 <label class="control-label" for="inputPassword">Password</label>                 <div class="controls">                     <input type="password" id="passwd" name="passwd" value="" placeholder="Password">                 </div>             </div>             <div class="control-group">                 <div class="controls">                     <label class="checkbox">                         <input type="checkbox"> Remember me                     </label>                     <input type="submit"  id="login" value="Login"class="btn btn-primary" />                  </div>             </div>         </form>               </div> </div> 

Here's the script I'm using:

$(document).ready(function(){     $('#myModal').modal('show'); });  function submitLogin() {     $.ajax({         url:"login_mysql.php",         type:'POST',         dataType:"json",         data: $("#login-form").serialize()     }).done(function(data){         //do something     }); }  $('#passwd').keypress(function(e) {     if (e.which == '13') {         submitLogin();     } });  $('#login').click(function(){     submitLogin(); }); 

On pressing enter after keying in my password or clicking on the submit button the same page reloads and the ajax script does not run. Could someone tell me if my script is clashing with the default settings of my modal window?

like image 691
Anon Avatar asked Oct 10 '13 13:10

Anon


People also ask

How do I submit a bootstrap modal form?

You CAN include a modal within a form. In the Bootstrap documentation it recommends the modal to be a "top level" element, but it still works within a form. You create a form, and then the modal "save" button will be a button of type="submit" to submit the form from within the modal.

How do you submit a form when a button is clicked?

In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.

How add modal in submit button?

Because the submit button is outside the form element. Wrap the form element around . modal-body - that will ensure that all the form fields and input buttons are in the form to be submitted. Also, apply some styling to the form with 0 padding and 0 margin as it may push the form making the modal look untidy.

How do you submit form only once after multiple clicking on submit?

onsubmit = function () { if (allowSubmit) allowSubmit = false; else return false; } })(); (well, as sure-fire as you can get with JS enabled anyway). You could disabled the button as a visual confirmation to the end user that the form can only be submit once too. Very correct.


1 Answers

Listen to form submit event - it gets triggered by both enter and click events.

Markup

<form id="yourForm">   <input type="text" name="username">   <input type="password" name="password">   <input type="submit" value="Submit"> </form> 

JS

$('#yourForm').submit(function(event){    // prevent default browser behaviour   event.preventDefault();    //do stuff with your form here   ...  }); 
like image 76
moonwave99 Avatar answered Sep 28 '22 03:09

moonwave99