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

  • Home
  • SEARCH
  • 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 7665053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:27:45+00:00 2026-05-31T14:27:45+00:00

I wish to authenticate a user each time they click a button. If the

  • 0

I wish to authenticate a user each time they click a button. If the user is logged out, and they attempt to click on a button, they are redirected home.

However the code after validate() is executes before the user is redirected home. In this example the popup always displays before the user is directed back home. How do I wait for the execution of validate() to complete before the rest of the event handler attempts to execute?

$("#main-display").on('click', 'button', function (event){
     validate();
     hello = window.open( '', '', "height = 100, width = 100");
     hello.document.write('hello');
});


function validate(){
    $.ajax({
        url: 'validate_user.php',
        dataType: "json",                      
        success: function(response) {            
            if(response.status == 'false') {
                location.href = 'home.php';
            }                
        }       
    });    
}

validate_user.php

<?php
    session_start();
    if(!isset($_SESSION['userId'])){
        session_destroy();
        $response['status'] = 'false';
    }
    else {
        $response['status'] = 'true';
    }
    echo json_encode($response);          
?>

EDIT

Thanks for the answers, however the popup still displays before the user is directed back even with the code bits of the most upvoted answers.

  • 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-05-31T14:27:46+00:00Added an answer on May 31, 2026 at 2:27 pm

    You could run your code in the ajax callback instead of directly after you start the request. What the below does is set up your validate function to take a single function parameter. This function is called after the AJAX has been run.

    You could even use else on line 14 to only run the code if they are valid users.

    $("#main-display").on('click', 'button', function (event){
         validate(function(){
             //some code here
         });
    
    });
    
    
    function validate(callback){
        $.ajax({
            url: 'validate_user.php',
            dataType: "json",                      
            success: function(response) {            
                if(response.status == 'false') {
                    location.href = 'home.php';
                }
                callback();
            }       
        });    
    }
    

    This is flexible as it allows validate to be used in different contexts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wish create a role for each user after that the user authenticate(login) to
I wish to authenticate a user without having the page reloaded, and having the
I wish to lock out access to a user's EDIT page (eg. /user/pure.krome/edit) if
I'm developing an iPhone application where I wish to authenticate (login form) on a
I wish to implement my software on a shareware basis, so that the user
I wish to have a home page with various widgets on them that display
I'm trying to work out how to validate a user in my application but
I wish to find out how long an operation takes in a Linux shell
I wish to call a method in my code behind from JavaScript, I sort
I wish to have a map from classes to instances of each class. The

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.