Reputation: 9
I am working on a project and am stuck. I am new to jQuery and JSON and I want to grab the current logged in user. I am able to pull the all the information from the user table, but I just want the user name so I can display it. I want to eventually append the current user to a html element
js code
var getUser = function(){
$.ajax({
url: 'xhr/get_user.php',
type: 'get',
dataType: 'json',
success: function(response){
if(response.error){
console.log(response.error);
}else{
console.log(response);
//$('#current_user').append(response);
}
}
});
};
php code
<?php
// check if logged in
// per project or all tasks?
//
error_reporting(E_ALL);
session_start();
session_regenerate_id(false);
require_once("reqs/common.php");
require_once("reqs/pdo.php");
//require_once("reqs/auth.php");
checkLoggedIn();
$userID = param($_GET, 'userID', $_SESSION["user"]);
$dbh = new PDB();
$db = $dbh->db;
$user = $dbh->getUser($userID);
exitjson(array("user"=>$user));
?>
Upvotes: 0
Views: 135
Reputation: 31
Just use $.getJSON
$.getJSON("xhr/get_user.php",function(userData){
console.log(userData);
}).fail(function(xhr,textStatus,err){
console.log("error",err);
});
And remember to add JSON type header to php respond.
An example of PHP:
header('Content-type: application/json');
echo json_encode($user);
Upvotes: 1