Below I want to confirm before deleting a record, problem here is whether I select ok or cancel from confirmation box record gets deleted on both, secondly I am not getting redirected to my required page after deletion.
<script type="text/javascript">
function ConfirmDelete(){
if (confirm("Delete Account?")){
window.location='mains.php';
}
else {
// do nothing
}
}
</script>
if(isset($_POST['submit'])){
$que=$db->prepare("DELETE FROM blogs WHERE id = :id");
$que->execute(array(':id'=>$postId));
}
<form method="POST">
<input type="submit" name="submit" value="Delete" onclick="ConfirmDelete()" />
</form>
so as basically the delete
button would submit the form
we can change the html code of the form to
<form action="mains.php" method="POST" onsubmit="return ConfirmDelete()">
<input type="submit" name="submit" value="Delete" />
</form>
and ConfirmDelete
function to
function ConfirmDelete(){
if (confirm("Delete Account?")){
return true;
}
else {
alert('sorry');
return false;
}
}
Typically I'd put an "action" on my form then a return false; in my javascript:
<form action="mains.php" method="POST" onclick="ConfirmDelete()">
<input type="submit" name="submit" value="Delete" />
</form>
Then js:
function ConfirmDelete() {
if (confirm("Delete account?")) {
return true;
}
return false;
}