Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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{

         }
      });
});
share|improve this question
See this thread: stackoverflow.com/questions/4240763/… – Tucker Jul 9 '12 at 15:42

2 Answers

up vote 2 down vote accepted

The username will be in the data object returned from $.get:

 $.get('login.php',params, function(data){
     if(data){
         alert(data.username); // that's the value from php
     }
 }

You can do a lot of checks on the value:

    ... 
    if(data.username) // username isn't empty string, null, undefined
    if(data.username!==undefined) // if you got it at all 
share|improve this answer
data.username is returning as undefined, can't figure out why – RapsFan1981 Jul 9 '12 at 20:06
are you getting a 200 response from your server? Check the web console (F12 in chrome) and look under network to see what your server is sending back. – bokonic Jul 9 '12 at 20:25
Yes I'm getting a 200. – RapsFan1981 Jul 9 '12 at 20:38
Using console.log(data) I see that it's correctly returning the value of "boolean" but username is null. $json = array("boolean" => "true", "username" => $username); – RapsFan1981 Jul 9 '12 at 21:37
I found my problem. For some reason I had this in my code: $username = $row["username"]; which was resetting the value of $username – RapsFan1981 Jul 9 '12 at 23:16

Use .getJSON(): http://api.jquery.com/jQuery.getJSON/

Then just use

data.username
share|improve this answer
I'm getting a return value of null when I do alert(data.username); (edit shown above) – RapsFan1981 Jul 9 '12 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.