简体   繁体   中英

Send an array to PHP script using AJAX call

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

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.

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/

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();
}
$.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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM