Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: window.location.reload() doesn't allow to work $.post()

Tags:

jquery

look at this script please

$("#change").click(function()
 {
  var val = $("#new_title").val();
  if(val == '')
  {
   alert("Նշեք խնդրեմ անունը");
   return false;
  }
  else
  {
   $.post
   (
    "change_title.php",
    {id: id, lang: lang, val: val}
   );

         window.location.reload();
  }
 });

where id and lang are global variables.

in change_title.php i'm uploading the table.

i want to show changes after editing, so i use window.location.reload(); function, but it doesn't work. if i delete window.location.reload(); function, it works fine.

what is the problem?

Thanks

like image 531
Simon Avatar asked Dec 10 '22 15:12

Simon


1 Answers

You need to run it after the $.post() completes, like this:

$.post("change_title.php",
       {id: id, lang: lang, val: val},
       function() {window.location.reload(); });

Without doing this as the callback to $.post() (this runs when it completes), the window is leaving the page before the POST completes. If you don't need to do anything else in that function, you can shorten it down to:

$.post("change_title.php",
       {id: id, lang: lang, val: val},
       window.location.reload);
like image 57
Nick Craver Avatar answered Jan 05 '23 06:01

Nick Craver