The essence of the problem: Function (x) sends a request to the php file, there it is processed and UPDATE happens in sql. After sending the request, the function (y) is called which updates the state directly in html, it makes a request to the database and sees what is there. So, when calling (x), the data on the page remains previous, that is, if we ..

write to database: 1 is displayed 0

write to database: 2 is displayed 1

and so on, I think the essence is clear. I thrust the function (y) in timeOut, but it does not save that what can be done in this situation?

function dbWrite(id, name) { var que = 'UPDATE test SET name = ' + name + ' WHERE id = ' + id; $.post( 'connect.php', {query: que}, function(answer) { }); setTimeout(test1(id), 1000); } function test1 (id) { $.post( 'connect.php', {id: id,}, function(workJson) { alert(workJson); }); } 
  • Something strange, you can decide as if your functions work in different connections to the database and at the same time after the update was not given commit - Mike
  • The code on the js side show - rjhdby
  • The code is attached, maybe you can not do that? - Vladimir Alexandrov
  • one
    First, you should not write a sql query on the client side. Hacking guaranteed. And secondly. Why on the server side it is impossible to record and return the sample at the same time? - Alexey Shimansky
  • because I am new and do not know much) - Vladimir Alexandrov

1 answer 1

No timeOut is needed, send an ajax request in asynchronous mode to update, wait for a response, then update the data on the page:

  $.ajax({ type: "POST", url: "/ajax.php", dataType: "json", success: function(data) { // здесь обновляем данные на странице } }); 

And all this must be done in one function and not in two different ones. And by the way, two requests are not very nice, if the first request returned without errors, just update the dom tree without the second request, with the data that you sent to the server for updating. Two requests is an extra load.