erltarregs erltarregs - 4 years ago 83
SQL Question

is there a way to prevent a form from submitting if I click "cancel" from the js confirm script that went before it?

I used the modal class in bootstrap, thru POST action to delete a record from the table, but it didn't have any confirmation to it so i tried using this confirm javascript.

The confirm works. The problem now is, even if I select "cancel" from the js confirm dialog box, it still deletes or routes to the delete.php file (which deletes the record) :o

Question: is there a way to prevent the form from submitting if I click "cancel" from the js confirm script? Thanks!

<!-- script and modal FROM THE HTML page users.php -->

<script>
function iconfirm() {
var r = confirm("Do you really want to delete this user? Press ok to confirm.");
if (r==true)
{
window.location="postDeleteUser.php";
return true;
}
else
{
alert("You pressed Cancel!");
window.location="users.php"
}
}
</script>

<div id="modDeleteUser" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modAddUser">
<div class="modal-dialog bs-example-modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h3 id="modAddUser">Delete User</h3>
</div>
<div class="modal-body">
<form class="" action="postDeleteUser.php" method="post">
<div class="control-group">
<label class="control-label" for="inputID">Input the User ID of the user you are deleting:</label>
<div class="controls">
<input type="text" class="input-small" id="inputID" placeholder="- ID No. -" name="txtuserid">
</div>
</div>
<!--<button type="submit">OK</button>-->
<button onclick="iconfirm()" type="submit">OK</button>
<!--======================-->

</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
</div>
</div>

<!-- FROM THE HTML page end -->




//=================== Delete.php start =========== //


<?php
$db_name="homeis";
$tbl_name="users";
$user_id=$_POST["txtuserid"];

include ("connect.php");
$deleteQuery= "DELETE FROM $tbl_name WHERE id='$user_id'";
$result = mysqli_query($db,$deleteQuery);

if ($db->query($deleteQuery) === true) {
echo "<script>alert('Data deleted!');window.location.href='users.php'</script>";
}
else {
echo "<script>alert('Unable to delete. Please try again.');window.location.href='users.php'</script>" . $conn->error;
}

$db->close();

?>

//=================== Delete.php end =========== //

Answer Source

You need to return false if you don't want the thing submitted..

add "return" here

<button onclick="return iconfirm()" type="submit">OK</button>

Then return something in the function. if you return true it will submit. if you return false, your computer might explode or something idk try it.

function iconfirm() {
      var r = confirm("Do you really want to delete this user? Press ok to confirm.");
      if (r==true)
        {
        window.location="postDeleteUser.php";
    return true;
    }
       else
    {
    alert("You pressed Cancel!");
    window.location="users.php"
    // return false here
    return false;
    }
 }

However, it might be easier to just..

<button onclick="return confirm('are you sure about whatever it is youre doing?')" type="submit">OK</button>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download