What he's suggesting is to encode the returning array (from the database query) as
JSON and have the JS code parse that. You
can use eval() to take the JSON string from the XHR result and 'objectify' it (that is, turn it into an object JS can use) or use the very handy (and more secure)
JSON parser (which I use). It should be trivial to run through each element in the object to populate the 'potential matches list'.
If the
PHP JSON functions aren't available to you, it's easy enough to create a JSON string and return it, e.g.
PHP Code:
// for example, the string sent to the PHP script to check is 'do'
// let's say this is a list of potential matches
$array= array(
'dodge ball', 'dogs', 'dog parks', 'douglas adams'
);
// begin the JSON object string and result list
$json= '{ list: [';
// run through each element in the array to populate the list
foreach ( $array AS $k => $v ) {
$json.= "'{$v}'";
$json.= ($k < sizeof($array) -1) ? ',' : NULL;
}
// end
$json.= ']}';
// output & exit for argument's sake
echo $json;
exit();
The resulting JSON object (indented by me for clarity):
Code:
{
list: [
'dodge ball',
'dog parks',
'dogs',
'douglas adams'
]
}