Alex
Alex

Reputation: 838

How to send an array with jQuery .ajax()

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 !

Upvotes: 0

Views: 777

Answers (3)

Rafay
Rafay

Reputation: 31033

 $.ajax({   
  url: path+'/inc/ajax/cars.php',
  data: {cars:$(cars).serializeArray()},
  cache: false,
  type: "POST",
  success : function(text){
       alert(text);
  }
 });

php

$arr = $_POST['cars'];

Upvotes: 0

Marc B
Marc B

Reputation: 360572

$php_array = json_decode($_POST['cars']);

Upvotes: 0

DrStrangeLove
DrStrangeLove

Reputation: 11557

try using php special array $_POST

Upvotes: 1

Related Questions