Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:34:15+00:00 2026-06-07T13:34:15+00:00

I have my simple jquery & php reg/login script working. Basically if the user’s

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:34:17+00:00Added an answer on June 7, 2026 at 1:34 pm

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple jQuery script that I'm trying to build upon but I
I have a very simple jQuery script.. all I want it to do is
I have a simple jquery cycle gallery populated by PHP. I would like to
all. I have built a simple jQuery/PHP chat program that works rather well. However,
I have a simple contact form which uses jquery to post to a php
I have made a simple chat script using jquery and AJAX but the huge
I'm working on a simple login form. When the user clicks on the Login
I have this jQuery simple code: <script type=text/javascript> <!-- jQuery(function(){ jQuery('#old_thumb').click(function(){ $(this).val($(this).attr('title')); $('#sf').val('2'); $('#add_new_event').submit();
I have a very simple jQuery/ajax script for a membership driven website. The script
Jquery puzzle I've got a php script that returns the name of random jpg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.