0

I am trying to consume a JSON array in PHP sent via JQuery. The data being sent to the server is in this format:

[{"id":"7","start":"00:00","end":"02:30","date":"2013-11-15"},{"id":"10","start":"23:00","end":"23:30","date":"2013-11-15"},{"id":"5","start":"13:00","end":"14:00","date":"2013-11-16"},{"id":"6","start":"18:00","end":"18:45","date":"2013-11-16"}]

I am sending the above data to the server through this function:

$('#updateOverallChanges').click(function(event){
 var formArray = new Array();
 $('.updateForm').each(function( index ) {
 formArray.push($(this).JSONFromSerialize());
 });
 $.ajax({
 url: 'inner/formTester.php',
 data: JSON.stringify(formArray),
 type: 'POST',
 contentType: "application/json; charset=utf-8",
 });
 });

The function JSONFromSerialize is responsible for converting the form in meaningful json data:

(function($) {
 $.fn.JSONFromSerialize = function() {
 var o = {};
 var a = this.serializeArray();
 $.each(a, function() {
 if (o[this.name]) {
 if (!o[this.name].push) {
 o[this.name] = [ o[this.name] ];
 }
 o[this.name].push(this.value || '');
 } else {
 o[this.name] = this.value || '';
 }
 });
 return o;
 };
})(jQuery);

I am using the following PHP code to process the JSON array:

<?php
$params = json_decode($_POST[]);
echo $params;
?>

But the above code give the following output:

Fatal error: Cannot use [] for reading in C:\xampp\htdocs\holidaymanager\inner\formTester.php on line 2

Second attempt

Modified the payload to be a name value pair:

 var postData = {
 data : formArray
 };
 $.ajax({
 url: 'inner/formTester.php',
 data: JSON.stringify(postData),
 type: 'POST',
 contentType: "application/json; charset=utf-8",
 });

The post data is:

{"data":[{"id":"7","start":"00:00","end":"02:30","date":"2013-11-15"},{"id":"10","start":"23:00","end":"23:30","date":"2013-11-15"},{"id":"5","start":"13:00","end":"14:00","date":"2013-11-16"},{"id":"6","start":"18:00","end":"18:45","date":"2013-11-16"}]}

The php code still fails:

<?php
$params = json_decode($_POST["data"]);
echo $params;
?>

Error:

Notice: Undefined index: data in C:\xampp\htdocs\holidaymanager\inner\formTester.php on line 2

Please advice how can I consume a JSON array sent over to the backend written in PHP.

asked Nov 18, 2013 at 10:15
1
  • Have you tried json_decode($_POST)? Commented Nov 18, 2013 at 10:17

1 Answer 1

2
 $.ajax({
 data : {data:JSON.stringify(postData)},
 })

in php

 $params = $_POST['data']; // add this line 
 $params = json_decode($params,true); 
 print_r($params);

or else

 $params = json_decode($_POST,true);
 print_r($params);

you have pass 2nd argument true with json_Decode()...

hope it may help you

answered Nov 18, 2013 at 10:20

6 Comments

I am getting this message: <br /> <b>Warning</b>: json_decode() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\holidaymanager\inner\formTester.php</b> on line <b>2</b><br />
the ajax request is sent like this: $.ajax({ url: 'inner/formTester.php', data : {'data':JSON.stringify(formArray)}, type: 'POST', contentType: "application/json; charset=utf-8", });
change 'data' to data
getting the same warning message
the payload seems to be messed up: data=%5B%7B%22id%22%3A%225%22%2C%22start%22%3A%2213%3A00%22%2C%22end%22%3A%2214%3A00%22%2C%22date%22%3A%222013-11-16%22%7D%2C%7B%22id%22%3A%226%22%2C%22start%22%3A%2218%3A00%22%2C%22end%22%3A%2218%3A45%22%2C%22date%22%3A%222013-11-16%22%7D%2C%7B%22id%22%3A%221%22%2C%22start%22%3A%2203%3A00%22%2C%22end%22%3A%2204%3A00%22%2C%22date%22%3A%222013-11-17%22%7D%2C%7B%22id%22%3A%222%22%2C%22start%22%3A%2212%3A00%22%2C%22end%22%3A%2212%3A30%22%2C%22date%22%3A%222013-11-17%22%7D%2C%7B%22id%22%3A%223%22%2C%22start%22%3A%2216%3A15%22%2C%22end%22%3A%...
|

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.