I have a jQuery script that makes an Ajax call to a PHP file (process.php) to retrieve the status of a database (The upload.php and import.php parts are working fine and not relevant here I would say), and I use javascript setInterval() to return the progress status value every second. Here is my jQuery script.
jquery.js
$(document).ready(function(){
var clear_timer;
$('#sample_form').on('submit', function(event){
event.preventDefault();
$.ajax({
url:"ajax/upload.php",
method:"POST",
//Additional Ajax parameters here
success:function(data){
if(data.success){
start_import();
clear_timer = setInterval(get_imported_data, 1000);
}
if(data.error){
//Error case code here
}
}
})
});
function start_import(){
$.ajax({
url:"ajax/import.php",
success:function(){
}
})
}
function get_imported_data(){
$.ajax({
url:"ajax/process.php",
success:function(data){
var total_data = $('#total_data').text();
var initial_rows = $('#initial_rows').text();
var width = Math.round(((data-initial_rows)/total_data)*100);
if(width >= 100){
clearInterval(clear_timer);
//Extra ajax parameters here
}
}
})
}
});
Here is my process.php script
session_start();
try{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$dbuser = "user";
$dbpass = "password";
$dbhost ="mysql:host=my_host;dbname=my_db;charset=utf8mb4";
$connect = new PDO($dbhost, $dbuser, $dbpass, $pdo_options);
}catch(Exception $e){
//Some Error handling here
}
$user_table = $_SESSION['user_table'];
$query = "SELECT * FROM $user_table";
$statement = $connect->prepare($query);
$statement->execute();
echo $statement->rowCount();
The progressbar looks like this if anybody is interested
The problem I have is that if I use session_start(); in the PHP file, the progress bar only displays at the very end of the process. If I remove session_start() and use static variables or constants, the progress bar displays correctly during all the process. I've searched extensively on various forums and can't find any posts like this that discuss the eventual link between session_start(); (in PHP) and setinterval(function(),delay); (in JS). The problem is that I need to be able to use a session variable in order to choose a table selected by the user. Any ideas on how to deal with this issue?

session_write_close()as soon as possiblesession_startopen the session file with a access lock, then if you start more scripts withsession_start, the second waits that the first script releases the lock. and this is released at the end of the script or withsession_write_close.