0

I have a jquery script that collects data from different sites and saves it through ajax and php to a sql database. At the moment the script saves each collection from a site separately. I want to change that, so that the script collects all the data in an array and when it is finished collecting it passes the array on to the php file which will save it in the database.

My problem now is, that I can't send the array after the "each loop" to the ajax statement. If I send the array inside the "each loop" it works (at the moment I save the data in a session on the php file, so that I can see it's structure and can adapt the pdo statement accordingly), but then I have a post request after each "loop". I want to avoid that, so that I have just one POST request with the whole array at the end. But if I use the ajax post request after the "each loop", the array I get is just empty. I thought I made the array global, so that I can use it outside the each statement, but for some reason it doesn't work.

Do you have any idea how to solve the problem? Here is the script:

$('#ScanButton, .ScanButton').click(function() {

var array = ["http://www.xyz.com/bla/bla/summary.html",
             "http://www.xyz.com/blu/blu/summary.html",
            ];

dataArray = [];

$.each(array, function(n, val) { 


$.get(val, function(res) { //get the html source of this website

  var data = {

  }

  dataArray.push(data);

// If I use the $.ajax statement here, it works and a correct array is submitted. But then I have a a POST request with each loop.. });

});

//If I use the $.ajax here, the array I get is just empty. Any Idea how to solve this?
data = YAHOO.lang.JSON.stringify(dataArray);      



  $.ajax({
    type:           'post',
    cache:          false,
    url:            'test.php',
    data:           {myJson:  data}
    });

  return false;

  });

Do you have any idea where the problem might be? If you need more information, please let me know. Thank you very much! :)

Sebsemillia
  • 9,366
  • 2
  • 55
  • 70

1 Answers1

1

[Edit] lol I'm an idiot, anyway, the reason your array is null is because the get requests are asynchronous, and are probably not completing before you send your final array. Either make your gets, synchronous, (probably not something you want to do) or wait to send the final array until all your gets have completed.

Andrew
  • 13,757
  • 13
  • 66
  • 84
  • your example is basically like mine. I changed the variable dataArr by taking the $. before it away.. but then I get an "undefined" in Firebug and still an empty array.. I guess the undefined appears because I didn't make dataArr globally by taking away the $. .. So I still have the same problem. btw: in your example the each loop is missing..;) Thank you for your help! – Sebsemillia Aug 09 '11 at 14:35
  • @sebsemilla You don't want it to be global, just declared outside the scope of the each loop. You should not be attaching it to the jQuery object. – Andrew Aug 09 '11 at 14:38
  • I changed the script in my question like I changed it now according to your recommendations.. But I still get an empty array.. Maybe you see a mistake I overlooked.. Thank you! – Sebsemillia Aug 09 '11 at 14:46
  • Hehe no problem! I thought this could be related to the asynchronous fact. But since this is my first work with ajax, I don't know much about that (yet).. Could you help me with some input how I could make my script make to wait until all the gets are completed? Thank you! :) – Sebsemillia Aug 09 '11 at 14:53
  • See [Link](http://stackoverflow.com/questions/5401969/jquery-ajax-how-to-wait-until-async-requests-success-completes-before-continui) – Andrew Aug 09 '11 at 15:01