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.