I am creating an array of objects in jQuery.
var selected_tests = $("#selected_tests").find("tr");
jsonLab = [];
$.each(selected_tests, function() {
jsonLab.push({
test: ($(this).children()).eq(0).text(),
amount: ($(this).children()).eq(1).text()
});
});
I am posting this array to a PHP file by
$('<input type="hidden" name="jsonLab"/>').val(jsonLab).appendTo('#bill_print');
$("#bill_print").submit();
In my PHP file
if(isset($_POST['jsonLab']))
{
$lab = json_decode($_POST['jsonLab'],true);
foreach ($lab as $key => $value) {
echo $value["test"] . ", " . $value["amount"] . "<br>";
}
}
There seems to be some mistake in the way I am using foreach or maybe it's incorrectly formatted JSON which isn't being decoded by PHP. I dont want to use AJAX for the submission.
$_POST['jsonLab']is or what your$valueare?print_r($_POST);stringifying your array before setting it to the hidden field.