dsdv76767671
2017-04-30 18:48
浏览 81
已采纳

在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.

图片转代码服务由CSDN问答提供 功能建议

我正在进行ajax调用,如下所示:

 <  code> $ .ajax({
 type:“POST”,
 url:“myPHP.php”,
 data:1,
 success:window.location.reload()
}); 
 <  / code>  
 
 

和一些与之相关的PHP:

 &lt;?php 
 
 // PDO CONNECTION 
 \  nsession_start(); 
 
 $ mission_id = $ _POST ['mission']; 
 
 $ _SESSION ['auth']  - &gt; mission_id = $ mission_id; 
 
if($ pdo-&gt; prepare(  'UPDATE client SET mission_id =?WHERE id = 1;') - &gt; execute([$ mission_id])){
 //创建一个会话,由页面加载时的标题读取
 $ _SESSION ['flash  ''['success'] ='好。'; 
}其他{
 $ _SESSION ['flash'] ['danger'] ='问题。'; 
} 
 
?&gt; 
    
 
 

在我的header.php(我在每个页面中都包含)中,我有一个代码来读取会话['flash'],显示相关的消息,然后销毁 它。 在我的页面中,我有一个php条件,它会照看会话['auth'] - &gt; mission_id并根据它显示内容。

我愿意刷新页面一次 php代码已完全完成,以便显示会话flash并考虑会话mission_id发生的条件。

问题是当我执行我的Ajax代码时 ,页面重新加载,但如果php会话(flash和mission_id)没有准备好。 我必须手动重新加载以查看更改。

我尝试使用完整而非成功的解决方案,甚至setTimeout但它不起作用。

我知道PHP有效,因为我可以看到我的数据库发生了PHP的变化。

我试过:

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

还有其他一些东西。

感谢您的帮助。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题