Jquery

Simple Example of Serialize of Object/Array in PHP and jQuery

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.

Simple Example of jQuery serialize Form to php array

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().

There are following method is used for serialization:

1 – serialize() : jQuery form serialize into Object

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 :

Related Post
"param1=someVal¶m2=someOtherVal"

2 – serializeArray() : jQuery form serialize into Array

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"
  },

3 – jQuery.param() : jQuery form concatenate and serialize into Object

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' 

How to get serialize values in php:

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
 )
These functions return a string representations of an array or an object which can than be decoded back into array/object.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

4 months ago

What is the Purpose of php_eol in PHP?

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

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories