0

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 !

3
  • cars is not array, it is an object. Javascript has no associative arrays. Commented Sep 22, 2011 at 19:15
  • stackoverflow.com/questions/299610/… Commented Sep 22, 2011 at 19:19
  • I found that I only had to create an object at first, not an array. The rest of my code was good : var cars = new Object(); cars['mom'] = "Ford"; cars['dad'] = "Honda"; Commented Sep 22, 2011 at 19:33

3 Answers 3

1

try using php special array $_POST

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

php

$arr = $_POST['cars'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.