I have a external js file handling deleting some element. According to the result, I would determine whether I need to refresh the page or not.
var deleted = 0; // first assume not deleted
$(function() {
$("#action a.action-delete").click(function() {
var id = $(this).parent().parent().attr("id");
$.get("modify-sale.php", { "id" : id, "action" : "delete" }, function (data) { deleted = 1; }, "text");
if (deleted) return true; // if success then refresh
else return false; // else does not refresh
});
No the problem is I could not change the global variable deleted in the jQuery event handler. I can assure that the delete action is success, but this variable just does not change its value to 1.
Why?
Ajax is asynchronous, so it will set the
deletedvariable after you do theif elsecheck. Try putting the check in the callback.