This simple tutorial help to serialize array and objects into jQuery and vise versa into PHP. PHP has in-built serialize and unserialize methods for serialize and deserialize object.
Serialization of object or php serialize array is very important operation in now days, because we are designing more responsive web application/mobile application. We need serialized array/object for ajax request as well as for web service. The target object can be JSON data object or simple form element. Serialization is used to storing or passing object values around without losing their type and structure.
The jQuery Serialize methods convert them into string and send as query string to server side. You can use de-serialization of string then you will get your own original object. There are many ways in jQuery to convert object in serialized object.
jQuery providing many methods for serialize object and into array.There are specific method for each serialize types.You can use serialize()
, serializeArray()
and jQuery.param()
.
This is common method to use send form values as a serialized object to server. The .serialize()
method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls.
$('#form').serialize()
This method send to server an object and server will received as below string :
"param1=someVal¶m2=someOtherVal"
This method behaves as like names, it is converting object in array manner. The .serializeArray()
method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls.
$('#form').serializeArray()
This method send to server an object and server will received as below string :
{ name: "param1", value: "someVal" }, { name: "param2", value: "someOtherVal" },
Sometimes when you do an Ajax form post in jQuery, you need to merge another object into your post data. The solution is to serialize the existing form data using jQuery’s $().serialize
method. Then we use the $.param()
utility method to create a serialized version of any JavaScript object or array. Simply concatenate the two objects.
data = $('#form').serializeArray()+ '&' + $.param('name':'phpflow');
This method send to server a object and server will received as below string :
a=%5Bparam1%5D&b=someVal¶m2%5D=someOtherVal&name='phpflow'
parse_str()
php method is use to convert query string into php array.serialize()
method is use for serialize object and unserialize()
for deserialize object.
$params = array(); parse_str($_GET, $params); print_r($searcharray); // You get any same of that Array ( [param1] => someVal [param2] => param2 )
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Thanks for submiting such a wonderful article. Great article. Thanks once again