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?
if(deleted) return truewill be executed before the Ajax callback is executed due to the asynchronous nature of Ajax. Hence: Have you checked the value with a debugger?