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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:03+00:00 2026-05-15T21:07:03+00:00

the question is fairly simple and technical: var it_works = false; $.post(some_file.php, ”, function(data)

  • 0

the question is fairly simple and technical:

var it_works = false;

$.post("some_file.php", '', function(data) {

     it_works = true;

});

alert(it_works); # false (yes, that 'alert' has to be here and not inside $.post itself)

What I want to achieve is:

alert(it_works); # true

Is there a way to do that? If not can $.post() return a value to be applied to it_works?

  • 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-15T21:07:03+00:00Added an answer on May 15, 2026 at 9:07 pm

    What you expect is the synchronous (blocking) type request.

    var it_works = false;
    
    jQuery.ajax({
      type: "POST",
      url: 'some_file.php',
      success: function (data) {
        it_works = true;
      }, 
      async: false // <- this turns it into synchronous
    });​
    
    // Execution is BLOCKED until request finishes.
    
    // it_works is available
    alert(it_works);
    

    Requests are asynchronous (non-blocking) by default which means that the browser won’t wait for them to be completed in order to continue its work. That’s why your alert got wrong result.

    Now, with jQuery.ajax you can optionally set the request to be synchronous, which means that the script will only continue to run after the request is finished.


    The RECOMMENDED way, however, is to refactor your code so that the data would be passed to a callback function as soon as the request is finished. This is preferred because blocking execution means blocking the UI which is unacceptable. Do it this way:

    $.post("some_file.php", '', function(data) {
        iDependOnMyParameter(data);
    });
    
    function iDependOnMyParameter(param) {
        // You should do your work here that depends on the result of the request!
        alert(param)
    }
    
    // All code here should be INDEPENDENT of the result of your AJAX request
    // ...
    

    Asynchronous programming is slightly more complicated because the consequence
    of making a request is encapsulated in a function instead of following the request statement. But the realtime behavior that the user experiences can be significantly
    better
    because they will not see a sluggish server or sluggish network cause the
    browser to act as though it had crashed. Synchronous programming is disrespectful
    and should not be employed in applications which are used by people.

    Douglas Crockford (YUI Blog)

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

Sidebar

Related Questions

I think this should be a fairly simple question for anyone who knows PHP.
I use PHP with CodeIgniter(MVC framework). My Question is fairly simple. What according to
I apologize in advance for the fairly simple question. I am familiar with PHP,
A fairly simple question for which I have a guess, but I can't find
Fairly simple question: I have an init method on my class that has the
fairly simple question and I haven't seen anything asked with this exact scenario laid
I believe this is a fairly simple question but it is something I am
I have a fairly simple question, however the answer seems to elude me. If
This should be a fairly simple question to answer, but I am interested to
I have a fairly simple question about these 2 templating engines. I'm trying to

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.