0

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{

         }
      });
});
1

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

4 Comments

data.username is returning as undefined, can't figure out why
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.
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);
I found my problem. For some reason I had this in my code: $username = $row["username"]; which was resetting the value of $username
1

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

Then just use

data.username

1 Comment

I'm getting a return value of null when I do alert(data.username); (edit shown above)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.