Title: Calling second XMLHttpRequest after first one XMLHttpRequest (failed)
Tags: javascript;php;mysql;ajax
Question: First one XMLHttpRequest is calling script to update MySQL database it can run multiple times depending on the situation. Script insertLocation will do the job, but in the end I am calling second XMLHttpRequest which should update file made from database. The problem is the second XMLHttpRequest will never read from updated database so there are data missing from first XMLHttpRequest.
However when I look to database, the data are there. I tried even sleeping before the creating of the file, but it did not helped. It seems like calling 2 XMLHttpRequest is not possible. I know about that third parameter of .open() , that when its set to false it will wait for request to be done, but it is now working. Here is my code:
insertToDatabase(lat,lng,id);
//there I tried 10 second sleep
updateDatabaseFile();
function updateDatabaseFile() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updateCoords.php",false);
xmlhttp.send();
alert("updated");
function insertToDatabase(lat,lng,id) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","insertLocation.php?lat="+lat+"&amp;lng="+lng+"&amp;id="+id,false);
xmlhttp.send();
alert("inserted")
One lats thing, when I run the updateDatabaseFile.php manually it will do the job. So I think the problem might be that changes done to database are made when this whole script ends so that is why I am getting incomplete data when reading.
EDIT: the alert("inserted") will open after the alert("updated") so there is the fail. The second one will finish first.
Comment: Why you run `updateDatabaseFile.php` manually while your code runs `updateCoords.php` ?

Comment: Your code appears to be just fine. Even though you are using a single `XMLHttpRequest` instance in both functions, it doesn't create unexpected results since your requests are synchronous. Check your backend code, or post it here.

Comment: Oh, and btw, the calling order of functions is different than how they appear in the file. It could be a silly mistake but you're supposed to call `insertToDatabase()` first, right?

Comment: Keeping the calls sync, what do you see in developer console? Any error, warning?

Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136838/discussion-between-ejaz-and-nocturne).

Comment: And what about the third parameter of .open() . I set to false so it should continue after its done, is that correct?

Comment: After this unsuccessfull script when I run It manualy the database is complete at this time so updateDatabaseFile.php will do what it should done

Comment: No I can not because the inserting to database can be called multiple times, and then when everything is inserted the database file should be created. But I have new info, when I do the alert in the `insertToDatabase` after open, the alert will open in the end after alert("udated") in `updateDatabaseFile`

Comment: yes, indeed I need to call insertToDatabase() first

Comment: I eddited the question, seems like the third parameter of .open() not working for me, and I need to add some `onreadyStateChange`

Comment: Console: Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. So there is the problem.

Comment: Can you tell me how to have asynchronous request with waiting to be done?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 12, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:02/28/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><php><mysql><ajax>', 'Title': 'Calling second XMLHttpRequest after first one XMLHttpRequest (failed)', 'ViewCount': 462, 'id': 42504678, 'user_AboutMe': '<p>ML enthusiast</p>\n', 'user_AccountId': 5222604.0, 'user_DisplayName': 'nocturne', 'user_Reputation': 597.0, 'user_Views': 213.0, 'user_id': 4175580.0}}