dsdv76767671
dsdv76767671
2017-04-30 18:48
浏览 45

在PHP Ajax调用结束时刷新当前页面

I'm having an ajax call which looks like this :

$.ajax({
  type: "POST",
  url: "myPHP.php",
  data: 1,
  success: window.location.reload()
});

And some php related to it :

<?php

// PDO CONNECTION

session_start();

$mission_id = $_POST['mission'];

$_SESSION['auth']->mission_id = $mission_id;

if($pdo->prepare('UPDATE client SET mission_id = ? WHERE id = 1;')->execute([$mission_id])) {
    // Creates a session to be read by the header at the page load
    $_SESSION['flash']['success'] = 'Good.';
} else {
    $_SESSION['flash']['danger'] = 'Problem.';
}

?>

In my header.php (that I include at each page), I have a code which reads the session['flash'], display the message associated, then destroy it. In my page, I have a php condition which look after the session['auth']->mission_id and display content according to it.

I'm willing to refresh the page once the php code has entirely finished in order for the session flash to be displayed and the conditions occured by the session mission_id to be taken into account.

The problem is that when I execute my Ajax code, the page reloads but like if the php session (flash and mission_id) were not ready. I have to reload manually to see the changes.

I tried solutions using complete instead of success, and even setTimeout but it didn't work.

I know the PHP works because I can see the change in my database occured by the PHP.

I tried :

$.ajax({
  type: "POST",
  url: "myPHP.php",
  data: myDatas,
  success: setTimeout(window.location.reload(), 3000)
});

And some other stuff.

Thank you for your help.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanlang1531
    duanlang1531 2017-04-30 18:55
    已采纳

    In both of these...

    success: window.location.reload()
    
    success: setTimeout(window.location.reload(), 3000)
    

    ...the parenthesis after reload are calling the method immediately. What's provided to either success: or setTimeout() is the method's return value, similar to:

    var _result = window.location.reload();
    
    $.ajax({
      type: "POST",
      url: "myPHP.php",
      data: 1,
      success: _result
    });
    

    To delay the call until the time of the event, you'll have to provide a function object directly. One option for doing that with methods like reload() is to wrap their call in another function.

    success: function () {
        window.location.reload();
    }
    

    Or:

    function onSuccess() {
        window.location.reload();
    }
    
    // ...
    success: onSuccess // no calling parenthesis
    

    In some cases, providing the method directly may also work.

    success: window.location.reload
    

    Though, doing so will often alter the meaning of this for the method and may require it to be bound beforehand.

    success: window.location.reload.bind(window.location)
    
    点赞 评论
  • duansaoguan7955
    duansaoguan7955 2017-04-30 18:58
    $.ajax({
        type: "POST",
        url: "myPHP.php",
        data: myDatas,
        success: function () {
            setTimeout(window.location.reload(), 3000)
        }
    });
    

    or

    $.ajax({
        type: "POST",
        url: "myPHP.php",
        data: myDatas,
        success: function () {
            window.location.reload()
        }
    });
    
    点赞 评论
  • dqqs64238
    dqqs64238 2017-04-30 19:48

    data:mydatas ! mydatas is the data you will send to the PHP script . so you can specify your variable name and data like this:

    var name=document.getElementById('name').value;
    data:"name="+name+"age="+age
    
    点赞 评论

相关推荐