0

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"];

MartiB
  • 1
  • 1
  • 3
  • 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 '13 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](http://php.net/manual/it/language.types.string.php) section of the manual for details) – GarethL Mar 25 '13 at 11:35
  • $items = $_POST["items"]; will help you – Ritesh Khatri Feb 22 '18 at 11:44

4 Answers4

0

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.

Community
  • 1
  • 1
scones
  • 3,317
  • 23
  • 34
  • 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 '13 at 12:36
0

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/

Community
  • 1
  • 1
JimCresswell
  • 164
  • 1
  • 3
0

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();
}
Gurmeet
  • 3,094
  • 4
  • 19
  • 43
0
$.ajax({

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

  data: {items:items}, 
  dataType: "html",

  success: function(msg)
  {
    //your code if call succeeds
  },
  error: function()
  {
    //alert("Failed"); 
  }
});

Note that the way you were using to pass the array was not right,get rid of the + and = sign and use : instead,Hope it helps!

Messy04
  • 17
  • 1
  • 8