Mostly JQuery receives response from the server as JSON data formate from PHP.
But some times we need to send bunch of data to the server. For that this example explains, how to send JSON data from JQuery to PHP.
From Jquery have to do the formate of objects push into array for the association then parse with JSON.stringify.
function send_user_details()
{
var row = new Array();
for (i = 0; i < count; i++)
{
var result = {};
result['name'] = 'set name field here';
result['phone_number'] = 'set phone_number field here';
row.push(result);
}
$.ajax({url: 'URL to the file',
data: {json_data: JSON.stringify(row)},
type: 'post',
success: function (output) {
alert(output);
}
});
}
Here, use json_decode method for formatted array data and set second parameter as TRUE to get as Associative Array.
function get_user_details() {
$user_details = json_decode($_POST['json_data'], TRUE);
$count = count($user_details);
for ($i = 0; $i < $count; $i++) {
$name = $user_details[$i]['img_opt'];
$phone_number = $user_details[$i]['img_src'];
}
# do further operations..
}