Is it possible to send back a json array along with seperate variables
Posted
by Scarface
on Stack Overflow
See other posts from Stack Overflow
or by Scarface
Published on 2010-05-01T15:25:17Z
Indexed on
2010/05/01
15:27 UTC
Read the original article
Hit count: 179
Hey guys I have an ajax jquery function that receives data from a php script. I want to return an array with all the online users which is retrieved from a mysql statement, and I want to send other separate variables I need for other purposes along with it. If anyone has any ideas, I would greatly appreciate it. NOTE: the example below is to illustrate what I want to do, I understand that json-encoding the array with other variables is dysfunctional.
JQUERY
$.ajax({
type: "POST",
data: "parameters",
url: "retrieval.php",
dataType: 'json',
success: function(json)
{
$('#div1').html(json.array);
$('#div2').html(json.variable1);
$('#div3').html(json.variable2);
}
})
PHP
$qryuserscount1="SELECT * FROM active_users";
$userscount1=mysql_query($qryuserscount1);
while ($row = mysql_fetch_array($userscount1)) {
$onlineuser= $row['username'];
$id=$row['id'];
$data[]=$onlineuser.$id; //for example there are 3 users, should send 3 entries back
}
$data['variable1']='something';
$data['variable2']='something else';
$out = json_encode($data);
print $out;
© Stack Overflow or respective owner