I have this simple jQuery function:
$(document).ready(function () {
var books = {};
books.id = '1';
books.author = 'Bob';
$.post('/index.php',
{
books: books
},
function(data, textStatus)
{
alert(data);
});
});
And this index PHP script:
<?php
foreach($_POST['books'] AS $key) {
echo ''.$key['id'].' is written by '.$key['author'].'';
}
?>
I want to loop through the jQuery array and display the id and author of each key in the array. I don't know the correct way to access the values in the array. It seems I'm doing it wrong.
books, not an array. you should also usejson_decodein the php to parse the data into an php objectvar books = [{id:1, author: 'Bob'}, {id:2, author: 'Bill'}]