coderatlarge
coderatlarge

Reputation: 649

Manual refresh vs. location.reload

I have a button (setarea) on my page which sets a session variable when clicked. There are a few other fields on the page that change based on this session variable. That's why whenever that button is clicked I want to automatically refresh the entire page so that the other fields get refreshed with the new value of the session variable. Here is my code:

        function setarea(){
            var area_id = jq("#splash_area").val();

            jq.ajax({
                type: 'POST',
                url: '<?php echo Mage::getBaseUrl(); ?>setsession/index/setarea',
                data: {area_id:area_id},
                success: function(data) {
                }
            });

            window.location.reload(true);

        }

Using this code, the page refreshes but the session variable does not refresh - it remains the same as before.

The interesting part is that when I do a manual refresh using F5 then the page refreshes correctly.

What am I doing wrong here?

Upvotes: 0

Views: 777

Answers (2)

user5051310
user5051310

Reputation:

Since AJAX is asynchronous, as soon as the AJAX request is made, reload fires immediately after. JS does not wait for a response to the AJAX request. Move the call to reload to inside the success function.

function setarea(){
  var area_id = jq("#splash_area").val();

  jq.ajax({
      type: 'POST',
      url: '<?php echo Mage::getBaseUrl(); ?>setsession/index/setarea',
      data: {area_id:area_id},
      success: function(data) {
          window.location.reload(true);
      }
  });
}

Upvotes: 2

Achilles
Achilles

Reputation: 441

try to replace

window.location.reload(true);

with

header("Location: YourFile.php");

Upvotes: 0

Related Questions