I have an upload feature that is made up of various different stages, and the duration of this upload can be quite lengthy. To increase usability of this feature, I want to present the user with information concerning the progress of the upload.
The upload is processed through jQuery, and uses AJAX to communicate with the DB.
To present different messages I am changing the value of a session varible (status) throughout different stages of my upload script. I am trying to use setInterval to check every second for the value of status, however this either doesn't present anything, or causes none of the upload code to work.
jQuery:
$('.content form').on('submit', function(e) {
e.preventDefault();
//get session value every second
setInterval(function() {
$.ajax({
url: "index.php?route=module/stock/getStatus&token=<?php echo $_GET['token']; ?>",
success:function(response){
console.log(response);
}
});
}, 1000);
//process upload
$.ajax({
url: $(this).attr('action'),
type: "POST",
data: form_data,
cache: false,
processData: false,
contentType: false,
success: function(data) {
data = $.parseJSON(data);
}
});
});
PHP used to retrieve session value (First AJAX Request):
echo json_encode($_SESSION['status']);
PHP used for upload (Second AJAX Request):
$get = $this->pdf2string($name); //get pdf data
$text = str_replace("\n", ",", $get); //change all new lines to commas
// process replacement strings
$replacements = array(
'Item No.' => 'sku',
'Description' => 'description,stock,discontinued',
);
$_SESSION['status'] = array(
'info' => 'Converting file to CSV'
);
foreach($replacements as $search => $replace) {
$text = str_replace($search, $replace, $text);
}
$_SESSION['status'] = array(
'info' => 'Structuring CSV data'
);
foreach($array as $item) {
// process csv data
}
$_SESSION['status'] = array(
'info' => 'CSV Structured'
);
$this->storeData($new_array); //store data
How can I use two AJAX requests at the same time?
I am using Windows 8.1 on XAMPP, however I have a test server which still produces the same results discussed above.
Any help would be great!
str_replace()can use arrays for the search and replace parameters dont you?