I’m trying to get a confirmation dialog to appear when a form is submitted, but I’ve actually been having quite a lot of difficulty in getting it to work.
Here’s what should happen:
- The user clicks the “purge logs” submit button
- A confirmation dialog appears
- If the user clicks the “yes” button, the form is submitted. If the user clicks the “no” button or “close” button, nothing happens.
Here’s what actually happens:
The forms submits itself without waiting for the user to click yes or no in the dialog that appears.
Here’s the code:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$().ready(function(){
$('form').submit(function(e){
var confirm = ConfirmDialog('Confirm', 'Are you sure?');
if(confirm){
form.submit();
}
});
function ConfirmDialog(title,message){
var confirmdialog = $('<div></div>').appendTo('body')
.html('<div><h6>'+message+'</h6></div>')
.dialog({
modal: true, title: title, zIndex: 10000, autoOpen: false,
width: 'auto', resizable: false,
buttons: {
Yes: function(){
$(this).dialog("close");
return true;
},
No: function(){
$(this).dialog("close");
return false;
}
},
close: function(event, ui){
$(this).remove();
return false;
}
});
return confirmdialog.dialog("open");
}
});
</script>
</head>
<body>
<form id="form_purge" name="form_purge" method="post" action="test.php">
<button type="submit" name="submit_purge" id="submit_purge">Purge Logs</button>
</form>
</html>