Show jquery ajax execution status
Let's say I make an ajax call to some cakePHP controller action to remove some data from the database. But before deleting data (which of course happens in my cakePHP controller action), I want to check if the user requesting data deletion has permission to do so. What I am trying to do is show the user the execution steps, for example I have something like this:
$.ajax({
type: 'POST',
url: '.../some_cakePHP_controller_action',
beforeSend: function(){
$("my_loading_div").html("Checking permissions...");
}
success: function(){
// do stuff on success
}
});
1- When user clicks DELETE I want to show him a loading div with a message like Checking permissions...
2- If the user has permission to delete data, the message changes to Removing data...
Now, as you can see above, the problem is that when the user presses DELETE, they will have a message Checking permissions....
, but my data might be deleted because I'm already in my cakePHP function. Thus, the user will have the wrong message. How can I achieve this behavior?
Thanks in advance for your help
source to share
You will need multiple ajax calls for this. The steps will be like this
- ajax call to check permission.
- In the php end, check if there is delete permission with is_writable .
- Returns the status of the previous step and shows it as ajax response
- According to the status of step 3. Send delete request to php using ajax
- Call unlink at the end of php. Then check again if file_exists exists
- Returns the status of the previous step and displays a message to the user.
source to share
You might want to make 2 ajax calls. First of all to check the permission and, if permission is available, then delete the item.
$("statusDiv").html("Checking permissions").fadeIn(100,function(){
$.post("checkpermission?item=23",function(data){
if(data=="allowed")
{
$("#statusDiv").html("Deleting...");
$.post("mycontroller/delete/23",function(result){
if(result=="deleted")
{
$("#statusDiv").html("Deleted Successfully");
}
});
});
});
source to share