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 8659639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:03:33+00:00 2026-06-12T16:03:33+00:00

I want to use script/ajax for my login system. So far I’ve been able

  • 0

I want to use script/ajax for my login system. So far I’ve been able to get back the array with all the errors and, if log in is successful set the $_SESSION[‘id’]:

<script type="text/javascript">
$(document).ready(function() {
$("input[type=button]").click(function () {  
var username = $('input[id=username]').val(); // get the username 
var password = $('input[id=password]').val(); // and the password
    if (username != '' || password !='') { // if not empty 

     $.ajax({
            type: "POST",
            url: "loginUser.php",
            data : "username="+username+"&password="+password,
            dataType: "json",
            success: function (data) {
            var success = data['success'];
            if(success == 'false'){
            var error = data['message'];
            alert(error); // the array with all the errors
             }else{
       $('#mask , .login-popup').fadeOut(300 , function() {
       $('#mask').remove();  
    });// end fadeOut function()
setTimeout("location.href = 'home.php';",1500);
    }
  }
  });//end success function

  } else {
alert('Enter some text ! '); // just in case somebody to click witout writing anything :)
    }
    });//end click function
});//end ready function
  </script>

loginUser.php basically check all the function and then send back the data like this:

if (empty($_POST)===false){
$email = sanitize($_POST['username']);
$password = sanitize($_POST['password']);


if (empty($email) === true || empty ($password) === true){ 

    $errors[] = 'You need to enter a username and password';
} else if (mail_exists($email) === false){
    $errors[] = 'we can\'t find that username. have you registered?';
}else if (user_active($email) === false){
    $errors[] = 'you haven\'t activated your account!';
}else {
$login = login($email, $password);
if ($login === false){
    $errors[] = 'username/password combination is incorrect!';
}else {
//set user session 
$_SESSION['id'] = $login;

//redirect user to home
    echo json_encode(array("success"=>'true'
                ));
}

   }
echo json_encode(array("success"=>'false',
            "message"=>$errors,             
            ));
                          }

as I said, I get ALL the alert if password and username are not correct and I get the $_SESSION set if password and username are correct but the popup stays shown and I don’t get redirect (but I can access because of the SESSION set). Is it correct to test if success is == true or == false???

***EDIT: fixed, the problem was in the php file. look at my answer….

  • 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-12T16:03:34+00:00Added an answer on June 12, 2026 at 4:03 pm

    fixed! the problem was in the if else logic in the php file:

    JS:

    <script type="text/javascript">
     $(document).ready(function() {
          $("input[type=button]").click(function () {
          var username = $('input[id=username]').val(); // get the content of what user typed ( in textarea ) 
          var password = $('input[id=password]').val(); // get the content of what user typed ( in textarea ) 
                                $.ajax({
                                type: "POST",
                                url: "loginUser.php",
                                data : "username="+username+"&password="+password,
                                dataType: "json",
                                success: function (data) {
                                var success = data['success'];
                                if(success == false){
            var error = data['message'];
                                alert(error); // just in case somebody to click on share witout writing anything :
    
    
                                    }
    
                                    if(success == true) {
       $('#mask , .login-popup').fadeOut(300 , function() {
       $('#mask').remove();  
                                    });// end fadeOut function()
        setTimeout("location.href = 'home.php';",1000);                                 
                                                    }
                                                        }
    
                            });//end ajax             
                     });//end click function
             });//end ready function
    

    loginUser.php:

       <?php
        if (empty($_POST)===false){
    $email = sanitize($_POST['username']);
    $password = sanitize($_POST['password']);
    
    if (empty($email) === true || empty ($password) === true){ 
                $result = false;
                $errors = 'You need to enter a username and password';
                echo json_encode(array("success"=>$result,
                                       "message"=>$errors,             
                                       ));
    } else if (mail_exists($email) === false){
                $result = false;
                $errors= 'we can\'t find that username. have you registered?';
                echo json_encode(array("success"=>$result,
                                       "message"=>$errors,             
                                 ));
    }else if (user_active($email) === false){
                $result = false;
                $errors = 'you haven\'t activated your account!';
                echo json_encode(array("success"=>$result,
                                "message"=>$errors,             
                                 ));
    }else {
    $login = login($email, $password);
    if ($login === false){
                $result = false;
                $errors = 'username/password combination is incorrect!';
                echo json_encode(array("success"=>$result,
                                "message"=>$errors,             
                                 ));
    } else {
    //set user session 
    $_SESSION['user_id'] = $login;
    $result = true;
        echo json_encode(array("success"=>$result
                                   ));
    
    }
    }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contact form i want to use Ajax for. My contact.php script
I want to use AJAX to process a simple login form. I thought it'd
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js type=text/javascript></script> I want to use the above ScriptTag in a SharePoint Visual
I want to use the Ajax-Upload script to upload multiple images. The kicker is
I have an ajax loaded content where i want to use a php script.
another day with Rails and today I want to use Ajax. linkt_remote_link for changing
Why does the JQuery script I use in all my pages seems to only
I want to make ajax call to get data for results nodes. In my
I want to use AJAX with .live() so i can make things neat. I
I'm trying to tell my watir script to wait for an ajax-injected login box

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.