Corfitz.
Corfitz.

Reputation: 1884

PHP return variables to AJAX (maybe an array?)

My wish is to send some information in a datastring through AJAX to a PHP page and then return variables for me to separate and ask jQuery to fill out in different elements. So that it would be able for me to just say:

$('.elemA').html($variableA);
$('.elemB').html($variableB); etc.

But I am not sure if it is possible or how to do it.. Maybe it can return an array for me to separate somehow? I don't know.


If anyone else finds this question - this script helped me out and actually showed me the meaning of the JSON encoding: http://www.jonsuh.com/demo/jquery-ajax-call-to-php-script-with-json-return

Upvotes: 0

Views: 143

Answers (1)

Shoe
Shoe

Reputation: 76280

You should use json_encode($array). Which uses JSON to encode your array and return a data string that can be read easily by Javascript.

To convert it to a Javascript array you can use parseJSON() or as suggested by @BishopZ use JSON.parse(ajaxResponse), which is built in most modern browsers.

Upvotes: 3

Related Questions