I just need a simple example of how to pass an array of strings to and from a php page using AJAX. Anyone wanna chime in?

Comments

grcm’s picture

I think you're looking at XML. Not hard to do- try google. It's not a Drupal-specific question.

On a more helpful note, if you have REALLY simple strings, you could just concatenate them with e.g. returns in between to pass back and forth.

But I think you'll find XML easier. Look at the example for whatever AJAX toolkit you are using.

-- Version Control your Drupal web site with The File High Club's Free Trial!

nedjo’s picture

Steps:

  • issue your Ajax request using the HTTPGet or HTTPPost functions included in drupal.js, e.g., HTTPPost(db.uri, db.receive, db, {'href' : db.href});
  • In your PHP handler, use drupal_to_js to convert your php data structure, e.g, an array, into Json format.
    $items = array();
    ...
    print drupal_to_js($items);
    
  • In the js function that receives the respons, use parseJson() to parse the response data into an object:
      var items = parseJson(string);
    

For some examples see the Javascript Tools package's modules, e.g., Activemenu.

Drupal doesn't however have built in tools to return the data from a JSON array or other data structure to PHP. For that you could use e.g. PEAR's Services_JSON package, http://pear.php.net/pepr/pepr-proposal-show.php?id=198