I am developing a chrome extension and I need to read information from a page, insert the data into the database table and then move on to the next page and do the same thing.
The problem is that the function which inserts the data (using ajax) inserts 6 rows out of 45 before moving to the next page, which means that we are moving to the next page without inserting the rest of the data.
What I want it to do is to respect code order and insert all the rows into the database and then move on to the next page. the code is as follows :
for (elt of an) {
var t = elt.getElementsByClassName('annonce_titre');
if ((elt.id !== '')) {
//console.log(().getElementsByTagName('a')[0].href);
let titleH2 = t[0].getElementsByTagName('h2');
let titleLink = t[0].getElementsByTagName('a');
var url = titleLink[0].href;
var title2 = titleH2[0].innerHTML;
var last_item = 0;
var w = elt.getElementsByClassName('titre_wilaya')[0].innerHTML;
console.log(w);
var wilaya = w.substring(w.length - 2, w.length);
console.log("leg0 leng " + (w.length - 2) + " ** " + w.length)
console.log("wilaya " + wilaya)
if (isNumber(wilaya)) {
var city = w.substring(0, w.length - 4);
} else {
var city = w;
wilaya = 16;
}
console.log("w c " + wilaya + " ** " + city)
var num = (elt.id).substring(4, 20)
// ADD DELAY OF 5 SECONDS BETWEEN PAGE LOADS
var logger = setInterval(logNextTitle, 10);
var inserts = [
[title2, wilaya, city, url, num]
];
test = test.concat(inserts);
console.log('test spead ');
$.ajax({
data: {
link: url,
h2: title2,
field: "auto",
last: last_item,
numero: num,
wilaya: wilaya,
city: city,
items: test
},
type: "post",
url: "http://localhost/insert.php",
success: function(data) {
console.log("Data Save: " + data);
}
});
}
}
//window.open(first_link ,'_SELF');
console.log("first_link " + first_link)
What this code does is loop through all the elements of an array , insert the data into the DB using ajax and then moving to the next page . https://i.stack.imgur.com/S0aFe.jpg
This console shows that echoing "first_link" in the code is after the code for insertion , but the later is executed after the echo. There is a miss-order in javascript