JavaScript location.reload() is losing post data

2019-02-17 16:54发布

I am trying to reload the page using java script the page reloads but the post data in the page is not loading the post data are deleted while the page reloads can any one help me with it

function currencychange(xxx) {
    setTimeout('delay()', 2000);
}
function delay(){
    location.reload();
}

this is the javascript code which I am using to reload the page onchange

7条回答
Lonely孤独者°
2楼-- · 2019-02-17 17:17

You can try this:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="form" id="form">

</form>

<script>
    // reload page every x miliseconds
    setInterval(function(){
    // inser here the post variables. Examples:

    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = 'id';
    input.value = <?=$id?>;

    document.form.appendChild(input);

    var input2 = document.createElement('input');
    input2.type = 'hidden';
    input2.name = 'anotherid';
    input2.value = <?=$anotherid?>;

    document.form.appendChild(input2);

    // send form
    document.form.submit();
    }, 120000);
</script>
查看更多
孤傲高冷的网名
3楼-- · 2019-02-17 17:31

This is a known bug in Chrome. The issue is that Chrome doesn't do a POST when you reload via JavaScript. If you did that using the reload button, it would behave properly and ask the user if he wants to repost the data.

All the details are here: http://code.google.com/p/chromium/issues/detail?id=6429 [Edit: related bug] https://bugs.webkit.org/show_bug.cgi?id=23735

PLEASE STAR THIS BUG SO THAT IT GETS FIXED FASTER.

查看更多
Lonely孤独者°
4楼-- · 2019-02-17 17:34

window.location.reload() issues a GET, so yes, the POST data will be lost.

The only ways you can issue a post are either:

  1. Use AJAX to post the data back, get the new page, and replace your body element with it, or

  2. Create a form element on the page with the action being the current window.location.href, and place the data you want to post back inside it as hidden inputs. Then, on currency change call form.submit()

查看更多
爷的心禁止访问
5楼-- · 2019-02-17 17:34

I solved the problem in this way:

// In the page called by POST, assuming coming from a search form 
// (i.e. searchResult.php)
$working=array();

if ($_POST) {
  if(!isset($_POST["postArray"]))  // Not set, first call
      $working = $_POST;
  else
     $working = unserialize($_POST["postArray"]); // recalled by someone else

  foreach ($working as $key => $value) { // Getting data
                $kv[] = "$key=$value";

// do something with input data....
           }

 echo '<form action="newRequest.php" method="post">'; 
// Your data here ......
 echo '<input type="hidden" name="currPost" value="' . htmlentities(serialize($working)) . '">';
 echo '<input type="submit" value="Go!">';
 echo '</form>';
 } // end if($_POST) ....


// This is the "newRequest.php" form
$postDataReceiver=array();
foreach ($_POST as $key => $value) { // Getting data from searchResult.php
           $kv[] = "$key=$value";

           switch($key) {
                // Your other stuff here ......
               case "currPost": // Here we are!
                    $postDataReceiver = unserialize($value);
                    break;


                 } // end switch
} // end foreach
echo '<form action="searchResult.php" method="post">'; 
// Your data here ......
 echo '<input type="hidden" name="postArray" value="' . htmlentities(serialize($postDataReceiver)) . '">';
 echo '<input type="submit" value="Go Back to search result!">';
 echo '</form>';

Just a little bit complicated, but it works. Hope this can help! Best, Luca.

查看更多
再贱就再见
6楼-- · 2019-02-17 17:35

I think you need to re-POST not re-load, as in HTTP POST rather than HTTP GET.

查看更多
We Are One
7楼-- · 2019-02-17 17:39

location.reload does not supposed to post any data. If you need to send your data back to server consider submitting the form with method='post' (if you have one), or use AJAX (e.g. jQuery.post)

查看更多
登录 后发表回答