I have my simple jquery & php reg/login script working. Basically if the user's name and pass is in the DB the php echoes out true and in my javascript I have
I want to pass the username back to my javascript and save it in a variable, how can I do that?
I think I can something like this in my php:
json_encode(array("boolean" => "true", "username" => '$username'));
but how do I handle it back in my jQuery?
$('#login').click(function(e){
$.get("login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data == "true"){
alert("Logged in");
$('#logindiv').slideUp();
$('#gamediv').show();
$('#registerdiv').hide();
$('#gameheader').html(userloggedin);
}else{
alert("Not Logged in");
}
});
});
EDIT: login.php
if (isset($_GET['username'])&& isset($_GET['password'])) {
$username = $_GET['username'];
$password = $_GET['password'];
$sql = mysql_query("SELECT * FROM members WHERE username='$username' AND password='$password'") or die(mysql_error());
// Get member ID into a session variable
$id = $row["id"];
session_register('id');
$_SESSION['id'] = $id;
// Get member username into a session variable
$username = $row["username"];
session_register('username');
$_SESSION['username'] = $username;
$json = array("boolean" => "true", "username" => $username);
echo(json_encode($json));
//exit();
} else {
echo "false";
}
main.html ...
$('#login').click(function(e){
$.getJSON("login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data.username!==undefined){
alert(data.username);
$('#logindiv').slideUp();
$('#gamediv').show();
$('#registerdiv').hide();
$('#gameheader').html(data['username']);
}else{
}
});
});