I've got an array like this one :
var cars = new Array();
cars['mom'] = "Ford";
cars['dad'] = "Honda";
I want to send the array cars via jQuery .ajax() function :
var oDate = new Date();
$.ajaxSetup({
cache: false
});
$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
$.ajax({
url: path+'/inc/ajax/cars.php',
data: {cars:cars},
cache: false,
type: "POST",
success : function(text){
alert(text);
}
});
How can I read the array on the server side, with my PHP script ?
Thanks !
asked Sep 22, 2011 at 19:09
Alex
8482 gold badges10 silver badges20 bronze badges
3 Answers 3
try using php special array $_POST
answered Sep 22, 2011 at 19:12
DrStrangeLove
11.6k16 gold badges63 silver badges73 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
$php_array = json_decode($_POST['cars']);
answered Sep 22, 2011 at 19:14
Marc B
362k44 gold badges433 silver badges508 bronze badges
Comments
$.ajax({
url: path+'/inc/ajax/cars.php',
data: {cars:$(cars).serializeArray()},
cache: false,
type: "POST",
success : function(text){
alert(text);
}
});
php
$arr = $_POST['cars'];
answered Sep 22, 2011 at 19:20
Rafay
31.1k5 gold badges71 silver badges106 bronze badges
Comments
default
carsis not array, it is an object. Javascript has no associative arrays.