1

I have a simple iron-ajax element like this:

<iron-ajax id="ajax_email"></iron-ajax>

Then later in javascript I add some parameters to the request, one of them being an array:

var request = this.$$("#ajax_email");
request.params.to = "test@test.com";
request.params.subject = "a cool test";
request.params.content = "some content";
var cc = ["test1@test.com", "test2@test.com", "test3@test.com"]
request.params.cc = cc;
request.generateRequest();

I have a simple PHP script that takes all these parameters, but can't figure out how to receive the "cc" array.

If I try with the GET method, iron-ajax generates the querystring like this:

url?cc=test1@test.com&cc=test2@test.com&cc=test3@test.com

instead of

url?cc[]=test1@test.com&cc[]=test2@test.com&cc[]=test3@test.com

So, $_GET["cc"] in PHP only gets the last value of the array, "test3@test.com".

When I try the POST method instead, $_POST is alway empty...

Anyone knows how to pass arrays with iron-ajax?

Hubert
  • 369
  • 3
  • 21

2 Answers2

1

You could use JSON.stringify(array) to turn your array into a string in JavaScript and parse it in an php object in your backend , using json_decode($_GET['array']). This should work with iron-ajax's params

kishtner
  • 11
  • 2
0

Ah well, I ended up doing a workaround with the PHP backend, to manually extract all the values from the query string. something like this:

$query = explode("&", $_SERVER['QUERY_STRING']);
$params = array();
foreach ($query as $param) {
  if(!empty($param)){
    $temp = explode('=', $param, 2);
    if(isset($temp[1]) && $temp[1] !== ""){
      list($name, $value) = explode('=', $param, 2);
      $params[$name][] = urldecode($value);
    }
  }
}
Hubert
  • 369
  • 3
  • 21