Hi, i’ve got this javascript code but I don’t understand why sweet alert doesn’t work, i hope someone could help me please.
function AnswerComment(userid,postid,gruppo) {
// Add event ID to the hidden field for future usage
$("#user_id").val(userid);
$("#comment_post").val(postid);
$("#user_group").val(gruppo);
// Open modal popup
$("#commentModal").modal("show");
}
$('#sendButton').click(function(AnswerComment) {
var userID = $('#user_id').val();
var postID = $('#comment_post').val();
var groupID = $('#user_group').val();
var desc = $('#comment_body').val();
$.ajax({
url: '../controllers/ctrl_admin_blog_app/ctrl_admin_comment_reply.php',
data: 'action=edit&userid='+userID+'&postid='+postID+'&groupid='+groupID+'&desc='+desc,
method: "POST",
success: function(data) {
swal({
title: "Evento eliminato!",
text: "L'evento è stato eliminato con successo.",
type: "success"
});
}
});
});
I don’t know sweet alert, but just copy/pasting that swal call to the console worked perfectly fine for me. So it might just mean that the success method doesn’t get called – have a look at the network panel in your browser dev tools to confirm that the request was successful in the first place.
Also, is that AnswerComment function supposed to be called somewhere? You’re just naming the event-argument in the click handler like that, which is probably not what you intended… thus there might be no userID etc. for the query string of the request URL.
Although a success response may be a “sure thing”, because sync can lock the browser I recommend adding code to deal with an error response just in case.
Synchronous AJAX calls essentially tell the browser “do this and wait until it’s done before doing anything else”
Fine as long as it gets done, but if for some reason it doesn’t the visitors experience could be less than ideal.
By having code that deals with a possible error response, even though the error is not desirable, at least it is a done and the browser can continue.