How can I retrieve a PHP variable from jQuery? If I am using a jQuery POST function such as this:
$.post("login.php", {username:username, password:password}, function(data){
$('section').html(data);
});
Instead of showing ‘data’ is it possible to show a certain PHP variable from the PHP file to output?
login.php:
would then return
helloas the contents of yourdatavariable in the ajax call. JS cannot directly access PHP variables, unless PHP choses to output those variables’ contents via the response to the ajax call. If you need to pass back multiple variables/values, then use a JSON container:would give you 3 pieces of data (‘hello’, ‘how’, ‘you’) which contain ‘there’, ‘are’, and ‘?’, respectively. You’d access them as
data['hello'],data['how'],data['you'].