nezarabd Posted December 27, 2016 Share Posted December 27, 2016 i want to get some data from mysql to use in phaser.io game i try this in start of game.js the ajax work fine but after the game.js end how make it work first when the game.js value = 1; wher = "a = 1"; var currentrec = []; $.ajax({ url: 'validate.php', type: 'post', dataType: 'json', data: {'method': "get_unit_lesson", 'value': value, 'wher': wher, 'tbl': "geography_prep_3"}, success: function (data) { currentrec = data; }, }); var dd = []; for (var s = 0; 0 < currentrec.length; s++) { dd = currentrec["unit"]} var game; Link to comment Share on other sites More sharing options...
squilibob Posted December 27, 2016 Share Posted December 27, 2016 The ajax call is asynchronous. You have to handle the promise callback value = 1; wher = "a = 1"; var currentrec = []; function validate() { return $.ajax({ url: 'validate.php', type: 'post', dataType: 'json', data: {'method': "get_unit_lesson", 'value': value, 'wher': wher, 'tbl': "geography_prep_3"}, success: function (data) { currentrec = data; }, }); } var successful = validate(); successful.done(function(){ var dd = []; for (var s = 0; 0 < currentrec.length; s++) { dd = currentrec["unit"]} var game; }); Link to comment Share on other sites More sharing options...
Recommended Posts