I have been looking around the internet for examples of an AJAX function which gets the latest messages from a database, I have been playing around with the code but I am stuck, currently I don’t know how I get the data from the php file to a variable in JQuery. sounds confusing maybe looking at my code will help.
any help is much appreciated.
JQuery code
$.ajax({
type: "GET", url: "include/process.php",
data:{
getLatestActivity: "true",
toUser: "4"
},
success: function(data){
alert("data: "+data);
}
});
php processing request
function getLatestActivity(){
global $session;
$data = $session->getLatestActivity(mysql_real_escape_string($_REQUEST['toUser']));
if($data){//successful
return $data;
}
}
The data function only prints out “data:”, Thanks in advance
In order to pass data from PHP to Javascript you need to
echoa string/json representation of what ever data you are trying to return. Note:echo json_encode($data);