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
-
3php arrays mean nothing to JS, but you can take a PHP array and json_encode() it, which jquery can take and convert to a javascript array.Marc B– Marc B2013年01月25日 16:49:34 +00:00Commented Jan 25, 2013 at 16:49
-
look into jquery ajax. it's very easy to use: api.jquery.com/jQuery.ajaxKai Qing– Kai Qing2013年01月25日 16:51:29 +00:00Commented Jan 25, 2013 at 16:51
1 Answer 1
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.