jQuery serializeArray( ) Method

« Previous Chapter Next Chapter »

Description:

The serializeArray( ) method serializes all forms and form elements like the .serialize() method but returns a JSON data structure for you to work with.

The JSON structure returned is not a string. You must use a plugin or third-party library to "stringify".

Syntax:

Here is the simple syntax to use this method:

Example

$.serializeArray( )
                                        

Parameters:

Here is the description of all the parameters used by this method:

  • NA

Example:

Assuming we have following PHP content in /jquery/serialize.html file:

Example

<?php
if( $_REQUEST["name"] )
{
   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
   $age = $_REQUEST['age'];
   echo "<br />Your age : ". $age;
   $sex = $_REQUEST['sex'];
   echo "<br />Your gender : ". $sex;
}
?>
                                        

Example

<html>
<head>
   <script type="text/javascript"  src="../../js/jquery-2.2.0.js"></script>
   <script type="text/javascript" language="javascript">
  $(document).ready(function() {
      $("#driver").click(function(event){
          $.post( 
             "/jquery/serialize.html",
             $("#testform").serializeArray(),
	             function(data) {
                $('#stage1').html(data);
             }
          );
          var fields = $("#testform").serializeArray();
          $("#stage2").empty();
          jQuery.each(fields, function(i, field){
            $("#stage2").append(field.value + " ");
          });
      });
   });
   </script>
</head>
<body>
   <p>Click on the button to load result.html file:</p>
   <div id="stage1" style="background-color:blue;">
          STAGE - 1
   </div>
   <br />
   <div id="stage2" style="background-color:blue;">
          STAGE - 2
   </div>
<form id="testform">
 <table>
 <tr>
 <td><p>Name:</p></td>
 <td><input type="text" name="name" size="40" /></td>
 </tr>
 <tr>
 <td><p>Age:</p></td>
 <td><input type="text" name="age" size="40" /></td>
 </tr>
  <tr>
 <td><p>Sex:</p></td>
 <td> <select name="sex">
   <option value="Male" selected>Male</option>
   <option value="Female" selected>Female</option>
   </select></td>
 </tr>
  <tr>
 <td colspan="2">
   <input type="button" id="driver" value="Load Data" />
 </td>
 </tr>  
</table>
</form>
</body>
</html>
                                        
Code It Online

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!