Tuesday, June 5, 2012

Jquery Serialize

I spent a while looking for a good way to pass variables from HTML to Javascript (ajax) to PHP. Turns out Jquery's serialize function is exactly what I needed.

Here's an example of the code:

//this will send all the inputs in the form to php with ajax
$('#form').submit( function() {
     var data = form.$('input').serialize();
     $.ajax({
          type: "POST",
          url: "process.php",
          data: data,
          success: function(msg){
          alert( "Your quote request has been submitted!" );
          }
     });
     return false;
} );

http://api.jquery.com/serialize/

Also, you can use serializeArray()to get the data in a better format. Here's a picture of the console.log output:

No comments:

Post a Comment