Mono Jamoon
Mono Jamoon

Reputation: 4607

Processing json array in PHP sent via jquery

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.

Upvotes: 0

Views: 502

Answers (1)

404 Not Found
404 Not Found

Reputation: 1223

   $.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

Upvotes: 2

Related Questions