1

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!

1
  • You do know that str_replace() can use arrays for the search and replace parameters dont you? Commented Jun 23, 2015 at 15:26

3 Answers 3

1

It sounds like your computer is not handling multiple connections. I have found a similar question on superuser.stackexchange - XAMPP apache not handling multiple requests

His solution is:

Starting and stopping the session every time I want to write to the session works though.

Without him supplying any code, I suspect he did something similar to:

session_start();
$_SESSION['status'] = array('info'=>'msg goes here');
session_write_close();
//...
session_start();
$_SESSION['status'] = array('info'=>'another msg goes here');
session_write_close();
//...
Sign up to request clarification or add additional context in comments.

Comments

1

There are several things to keep in mind:

  • Your browser will limit the number of simultaneous requests. This depends on the browser and can be anywhere between 2 and 8.

  • The server may lock the request while waiting for a resource (that is locked by another request). If you're using sessions at all make sure you close the session as soon as possible or will cause simultaneous requests to wait.

  • You might want to look at push notifications (instead of pull) to get progress data. You can use websockets or eventsource.

Comments

1

Ok, i think setInterval is a bit messy cause it executes on a timed interval even if previous code is on an i/o wait or network delay. the second thing is that you have to release it after the intended task is complete.

So i modified your code a bit,

var status_fun=function() {
    $.ajax({
        url: "index.php?route=module/stock/getStatus&token=<?php echo $_GET['token']; ?>",
        success:function(response){
          console.log(response);
          /////////////////////////////////////////////////////////
          //Add Something to quit this timed loop after completion
          //And please do handle ajax error case too
          setTimeout(status_fun,1000);
          /////////////////////////////////////////////////////////
        }
    });
  };






$('.content form').on('submit', function(e) {
  e.preventDefault();


  status_fun();

  $.ajax({
    url: $(this).attr('action'),
    type: "POST",
    data: form_data,
    cache: false,
    processData: false,
    contentType: false,
    success: function(data) {
      data = $.parseJSON(data);
    }
  });




});

hope it helps, cause i am not certain about your goal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.