i have a ajax that needs to return some values, but if i use a while() to get all the result from my db, the return is “nothing”.
js:
$.post("ajax/file.ajax.php",
{
cache: false
},
function(data){
alert(data.region);
},'json'
);
php:
while(!$res->EOF)
{
header('Content-Type: application/json');
$return_data = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
echo json_encode($return_data);
$res->MoveNext();
}
so, how can i use the while with json ?
The easiest solution would be to create an array and then
array.pushthe row you are generating onto the end of it.For example:
PHP
JS:
Then output the json encoded array to finish.
You should also move your header out of the while loop and put it just before you echo the new array.