Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm quite new to ajax, I'm not able to solve this problem and I can't find other topics discussing about it. What I have to do is send with ajax an array to a php script.
The array is an associative array [index][value]. The problem is that, once I've sent the array to php, it seems like a monodimensional array. In other words, an example:
if the array is: ["apple", "pear", "orange"]
should be: array[0] prints "apple"

BUT in php the array consists in only one element, which is the concatenation of all the strings. So if I print array[1] I'll obtain "p", array[4] "e", etc.
How can I fix it?

Thank you in advance for your help.

var items = new Array();

CODE AJAX SCRIPT:

    $.ajax({

      type: "POST",
      url: "calculate.php",

      data: "items=" + items, 
      dataType: "html",

      success: function(msg)
      {
        var response = jQuery.parseJSON(msg);
        $('#second_results').html(response.output); 
      },
      error: function()
      {
        alert("Failed"); 
      }
    });

PHP:

$items = $_REQUEST["items"];

share|improve this question
2  
I wonder if it's the datatype, should that be JSON? What is the browser actually sending to PHP? e.g. use Chromes DeveloperTools/network tab to find out. – Mark Mar 25 at 11:24
Whatever it is you are sending to PHP, PHP is interpreting it as a string ($a = "apple"; echo $a[0]; will print "a". See the String access and modification by character section of the manual for details) – GarethL Mar 25 at 11:35

3 Answers

You have several choices here. Amongst others i present 2 of those.

1)

comma separate the parameters and split at the comma.

// ...
data: "items=" + item1 + "," + item2 + "," item3,
// ...

$items = explode(',', $_REQUEST['items']);

2)

use the other notation:

// ...
data: "items[0]=" + item1 + "&items[1]=" + item2 + "&items[2]=" + item3,
// ...

$items = $_REQUEST['items'];

Althought i haven't tested either, it should work in general. :)

also you might want to take a look at: Parse query string into an array to let php handle the correct conversions.

share|improve this answer
Thak you for the answer :) the problem is that I add elements to the array dinamically. So the soulution could be iterate on the array, create a variable containing all the elements like you suggest, and then use "data: myVariable". Could it work? I try now! – MartiB Mar 25 at 12:36

There are also a variety of methods here: Pass array to ajax request in $.ajax() . There is also a nicely annotated example here http://www.islandsmooth.com/2010/04/send-and-receive-json-data-using-ajax-jquery-and-php/

share|improve this answer

Pass this in data of ajax call:

        var a = {};
        a["key1"] = "val1";
        a["key2"] = "val2";
        a["key3"] = "val3";
$.ajax({

  type: "POST",
  url: "calculate.php",

  data: a ,
  dataType: "html",

  success: function(msg)
  {
    var response = jQuery.parseJSON(msg);
    $('#second_results').html(response.output); 
  },
  error: function()
  {
    alert("Failed"); 
  }
});

On Php Side:

 if($_SERVER["REQUEST_METHOD"]=="POST")
{
   foreach($_POST as $key=> $val){
   echo $key."and".$val;
   }
    die();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.