0

I am trying to pass a Json obtained by a json.stringify process of an html table to a php file to insert it into a database. First,wanted to check is the code below is ok : i get the right output of the json when doing an alert of 'myjson':

var myjson=  JSON.stringify(mydata);
alert(myjson);

[{},{"product[]":"john","qty[]":"288","price[]":"199","total[]":"57312"},{"product[]":"sylvia","qty[]":"12","price[]":"13","total[]":"156"},{"product[]":"juan","qty[]":"11","price[]":"9","total[]":"99"},{"total_amount":"57567.00"}]

then i have this ajax to send it to php (test.php):

$.ajax({
url: "test.php",
type: "POST",
data: myjson,
dataType: "JSON",
success: function (data) {
    alert(data);

}

});

And my php file to check if the output is fine:

$obj = json_decode($_POST["mydata"]);

echo $obj->var;

but i do not get anything in my alert once the php is supposedly processed?

what is wrong?

R_life_R
  • 786
  • 6
  • 26

1 Answers1

0

Firstly, you will need to post the data with a name, more specifically with the name "mydata".

Currently the PHP looking for a post called mydata ($_POST["mydata"]) which is not available and throws the following Notice Undefined index: mydata

To do that, you can change the data you're sending from:

data: myjson

to

data: {"mydata": myjson}

Second, the data sent to the back-end is an array of objects

Array
(
    [0] => stdClass Object
        (
        )

    [1] => stdClass Object
        (
            [product[]] => john
            [qty[]] => 288
            [price[]] => 199
            [total[]] => 57312
        )

    [2] => stdClass Object
        (
            [product[]] => sylvia
            [qty[]] => 12
            [price[]] => 13
            [total[]] => 156
        )

    [3] => stdClass Object
        (
            [product[]] => juan
            [qty[]] => 11
            [price[]] => 9
            [total[]] => 99
        )

    [4] => stdClass Object
        (
            [total_amount] => 57567.00
        )

)

You will need to access them by index or iterate through the array.

Access them by index: $obj[0]->var or $obj[1]->var

Last but not least, the properties contain square brackets {"product[]":"john","qty[]":"288","price[]":"199","total[]":"57312"} which is not recommended but will still work. Accessing the properties should be done like: $obj[1]->{'property[]'}

JavaScript:

<script type="text/javascript">
    function  sendAjax(){
        var mydata = [{},    {"product[]":"john","qty[]":"288","price[]":"199","total[]":"57312"},    {"product[]":"sylvia","qty[]":"12","price[]":"13","total[]":"156"},{"product[]":"juan","qty[]":"11","price[]":"9","total[]":"99"},{"total_amount":"57567.00"}]
        var myjson =  JSON.stringify(mydata);

        $.ajax({
            url: "test.php",
            type: "POST",
            data: {"mydata": myjson},
            dataType: "JSON",
            success: function (data) {
                alert(data);
            }
        });
    }
</script>

PHP:

<?php
    $obj = json_decode($_POST["mydata"]);
    echo $obj[1]->{'product[]'};
?>

Update:

The AJAX is expecting a JSON response, otherwise it will fail. In the Back-End you will need to change the response to JSON.

<?php
    $obj = json_decode($_POST["mydata"]);
    header('Content-Type: application/json');

    echo json_encode($obj[1]->{'product[]'});
?>
Attila Antal
  • 811
  • 8
  • 17