Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing a form submit with jquery and .submit

I'm attempting to use jQuery to capture a submit event and then send the form elements formatted as JSON to a PHP page. I'm having issues capturing the submit though, I started with a .click() event but moved to the .submit() one instead.

I now have the following trimmed down code.

HTML

<form method="POST" id="login_form">     <label>Username:</label>     <input type="text" name="username" id="username"/>     <label>Password:</label>     <input type="password" name="password" id="password"/>     <input type="submit" value="Submit" name="submit" class="submit" id="submit" /> </form> 

Javascript

$('#login_form').submit(function() {     var data = $("#login_form :input").serializeArray();     alert('Handler for .submit() called.'); }); 
like image 691
Nathan Avatar asked Sep 03 '12 18:09

Nathan


People also ask

How can we submit a form using jQuery?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.

How do you check form is submitted or not in jQuery?

$("#something-%"). submit(function(e) { e. preventDefault(); $("#some-span"). html(data); });

How can I get form data with JavaScript jQuery?

The serializeArray() method creates an array of objects (name and value) by serializing form values. This method can be used to get the form data. Parameter: It does not accept any parameter.


2 Answers

Wrap the code in document ready and prevent the default submit action:

$(function() { //shorthand document.ready function     $('#login_form').on('submit', function(e) { //use on if jQuery 1.7+         e.preventDefault();  //prevent form from submitting         var data = $("#login_form :input").serializeArray();         console.log(data); //use the console for debugging, F12 in Chrome, not alerts     }); }); 
like image 120
adeneo Avatar answered Nov 07 '22 00:11

adeneo


try this:

Use ´return false´ for to cut the flow of the event:

$('#login_form').submit(function() {     var data = $("#login_form :input").serializeArray();     alert('Handler for .submit() called.');     return false;  // <- cancel event }); 

Edit

corroborate if the form element with the 'length' of jQuery:

alert($('#login_form').length) // if is == 0, not found form $('#login_form').submit(function() {     var data = $("#login_form :input").serializeArray();     alert('Handler for .submit() called.');     return false;  // <- cancel event }); 

OR:

it waits for the DOM is ready:

jQuery(function() {      alert($('#login_form').length) // if is == 0, not found form     $('#login_form').submit(function() {         var data = $("#login_form :input").serializeArray();         alert('Handler for .submit() called.');         return false;  // <- cancel event     });  }); 

Do you put your code inside the event "ready" the document or after the DOM is ready?

like image 40
andres descalzo Avatar answered Nov 06 '22 23:11

andres descalzo