-1

I am trying to create a login form. The issue is it will only execute the action when Someone hits the submit button, Im trying to add the functionality if the user has clicked the input

field then hits enter (just like it would without jquery and normal php) it will also execute the same code to check for errors or goto a post veriable that I am testing it with.

Heres the full JQuery code which goes to A PHP $_POST page todo testing..

$(document).ready(function() {


$('#submit').click(function() {

      var user_login = $('#user_login').val();
      var password = $('#pass_login').val();


$.post('ajax/check.php', {user_login: user_login, password: password}, function(data) { 
$('#content').html(data);

} );


});
Spudster
  • 69
  • 1
  • 11

1 Answers1

0

You could set up multiple event handlers and call a validation function from those event handlers. You may also combine the following code to one event handler callback.

var form = document.form['my-form'];

/* submit form via return-key */
form.on( 'keypress', 'input,button', function( event ) {
    if( event.keyCode == 13 ) {
        event.preventDefault();

        submitForm( form );
    }
} );

/* submit form via button click */
form.on( 'click', '[type=submit]', function( event ) {
    event.preventDefault();

    submitForm( form );
} );

/* disable default form submit */
form.on( 'submit', function( event ) {
    event.preventDefault();
    throw new Error( 'This form can\'t be submitted using submit(), we\'re using AJAX here.' );
} );

function submitForm( form )
{
    /* validation code here */
    /* … */

    /* then send AJAX request */
    /* … */
}
feeela
  • 29,399
  • 7
  • 59
  • 71
  • I could try that seems like the best idea. Your code doesen't seem to work, What do I configure in your code to make it work with my input fields? – Spudster May 25 '13 at 09:23
  • Ok I took your advice of doing two event handlers and its working out great so far only issue is now is when A user holds down enter they can send high traffic to A single file which could lag out the DNS Server. If I have any problems I will reply heree. Update: Nevermind did it with keyup instead of keydown instead. Thanks ;.) – Spudster May 25 '13 at 11:30