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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:13:34+00:00 2026-06-06T07:13:34+00:00

I have a page with some user selectable options and a button that, when

  • 0

I have a page with some user selectable options and a button that, when clicked, runs a PHP script and then refreshes a div with another PHP file that uses a session variable that is created at the end of the first PHP script. If the user presses the button again, with different options selected, the div is updated using the newly replaced session variable. The problem is that sometimes, perhaps 1 in 10 times or so, the old session variable data is loaded. I suspect that the second PHP file is catching the variable too early, before it has been updated, but I tried unsetting the session variable at various points with out any luck.

First PHP file:

session_start();
$needle = array();
foreach($_POST['checkboxes'] as $key => $value){
$needle[] = "$value";
}
// code that processes the values from needle and outputs $data
unset($_SESSION['data']);
$_SESSION['data']=$data;

Second PHP file:

session_start();
echo $_SESSION['data'];

Javascript:

$(".userdata").click(function() {
   $.post("first.php", $("form#checkboxes").serialize());
});
$(function() {
   $("#button").click(function() {
   $("#div").load('second.php')
   })
})
  • 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-06T07:13:36+00:00Added an answer on June 6, 2026 at 7:13 am

    The problem is that in some cases the first PHP script has not finished running before you click the button that loads the second PHP script (like I implied before in my comment). The fact that this happens is related to how scripts are scheduled by the webserver (which is a different subject entirely).

    You thus need to make sure that when you click the button that runs the second script, the first script has completely finished running.

    Because in my knowledge, javascript does not allow blocking/signaling on a variable (like Java does), you’ll have to use a more ‘dirty’ technique called busy waiting.

    The best way to do this, is to include an extra variable in the javascript you are using.

    var wait = false;
    function reloadSecond (){
      if (wait){
        setTimeout('reloadSecond()',200);
      } else {
        $("#div").load('second.php');
      }  
    }
    
    $(".userdata").click(function() {
       wait = true;
       $.post("first.php", $("form#checkboxes").serialize(), function(){
         wait = false;
       });
    
    });
    $(function() {
       $("#button").click(reloadSecond);
    })
    

    While ‘busy waiting’ is generally not considered the most elegant solution, I think you don’t have many other options in this case (except for serverside push, which is much more complicated). Additionally, you’ll probably only incur the extra 200 millisecond (or less, you can of course change this value) waiting time once or twice.

    (side note: I assume that javascript is single threaded here, which is true in almost all cases: Is JavaScript guaranteed to be single-threaded?).

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

Sidebar

Related Questions

I have a web page that prompts for user input via DropDownLists in some
I have developed some script for detecting when user scroll down a page, and
I have a page with some elements that are controlled by the user. One
I have a page with some links inside a div and some outside that
I want to have a page run some PHP code when a user clicks
I have a page with some generated HTML that survives the form's reset button.
I have a jsp page that is trying to reference some user defined classes.
I have to create a sample silver-light page that has some user interactive elements
My situation is that, I have page contains a button and some links. Whenever
I have a page where a user can upload a file along with some

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.