1

I have jquery code output json data and I need to send this data to to url and fetch using php

Code:

JSON.stringify(showtimes)

Output:

[{"show_id":"1","movie_id":"20","cinema_id":"10","status":"0","times":"00:00"},{"show_id":"2","movie_id":"21","cinema_id":"11","status":"1","times":"01:00"}, ... etc]

I need to send this json data JSON.stringify(showtimes) via ajax to url and fetch json using php.

My ajax code:

$.ajax({
 type: 'POST',
 url: '/admin/save/',
 data: JSON.stringify(showtimes), 
 success: function(data){}
});

Question:

  • Check ajax code is true ?
  • How fetch data using PHP ?
asked Jun 8, 2012 at 14:11

3 Answers 3

3

Firstly, you should assign a data key to your json object in the javascript:

$.ajax({
 type: 'POST',
 url: '/admin/save/',
 data: {json : JSON.stringify(showtimes)}, 
 success: function(data){}
});

Within your PHP would would access the JSON string as:

$_POST['json'];

Secondly, within PHP you will want to gather your data using the following example:

$json = json_decode($_POST['json'], true);

You can then access your JSON array as an associative array:

foreach($json as $show_details){
 $show_id = $show_details['show_id'];
 ...
}
answered Jun 8, 2012 at 14:20
Sign up to request clarification or add additional context in comments.

Comments

1

You can check if the ajax response valid JSON by using something like below:

function isValidJSON($request_data)
{
 return (json_decode($request_data) != NULL) ? TRUE : FALSE;
}

You can then use json_decode() to decode the data.

answered Jun 8, 2012 at 14:18

Comments

0

To decode json to php array use json_decode.

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
 //It is ajax
}
answered Jun 8, 2012 at 14:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.