You will end up with object looking like:
var fruit ={
name:'Apple',
color: 'Red'
}
You can simply post that object to php
$.post('path/to/server', fruit, function(resp){
// validate response and do something
});
Default contentType for $.ajax
of which $.post
is a shorthand method is application/x-www-form-urlencoded
so it is exactly the same as sending a form.
jQuery internals will take care of encoding the data object passed as argument to $.ajax
or $.post
or other shorthand methods
In PHP
$fruitName = $_POST['name'];
$fruitColor = $_POST['color'];
If you wanted to send the whole array you could do:
$.post(url, {fruits: fruits});
Then in php:
$fruitsArray = $_POST['fruits'];
foreach($fruitsArray as $item){
$name = $item['name'];
}