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[]'});
?>