Reputation: 198
<div class="report" data-id="55"></div>
<script>
$('.report').click(function() {
var id = $(this).data("id");
confirm("do you really want to report this post?");
$.ajax({
url: 'forumreport.php',
type: 'GET',
data: 'id='+id,
success: function(){
alert("reported!!");
},
});
})
</script>
I want to stop $.ajax()
method if user clicks cancel on confirm form. However ajax is still sending the id to .php page. What should i do?
Upvotes: 1
Views: 1505
Reputation: 92854
Confirm
function returns a boolean value indicating whether OK or Cancel was selected (true
means OK)
https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm
Check for result which is returned by confirm
function:
<script>
$('.report').click(function() {
var id = $(this).data("id");
if(confirm("do you really want to report this post?")){
$.ajax({
url: 'forumreport.php',
type: 'GET',
data: 'id='+id,
success: function(){
alert("reported!!");
},
});
};
})
</script>
Upvotes: 0
Reputation: 240898
The confirm
method returns a boolean indicating whether the prompt is confirmed, therefore you can simply save the resulting boolean in a variable and check whether it is true before executing the AJAX request.
$('.report').click(function() {
var id = $(this).data("id");
var confirmed = confirm("do you really want to report this post?");
if (confirmed) {
$.ajax({
url: 'forumreport.php',
type: 'GET',
data: 'id='+id,
success: function(){
alert("reported!!");
},
});
}
});
Upvotes: 1