I want to make an "Are you sure?" button before any changes to my database are made.
The form and code that I use are the following, can anyone help me with this cause I can't figure it out
<form method="post" action="php_code.php" >
<div >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<label>Inkoopprijs</label>
<label>
<input type="number" step="any" name="inkoopprijs" value="">
</label>
</div>
<div >
<label>Verkoopprijs</label>
<label>
<input type="number" step="any" name="verkoopprijs" value="">
</label>
</div>
<div id="product" >
<label>product</label>
<label>
<input type="text" name="producten" value="">
</label>
</div>
<div >
<?php if ($update == true):?>
<button onclick="update_time" type="submit" name="update" style="background: darkorange;" >Update</button>
<?php else: ?>
<button type="submit" name="save" >Save</button>
<?php endif ?>
<a href="index.php">Home</a>
</div>
</form>
if (isset($_POST['update'])) {
$id = $_POST['id'];
$inkoopprijs = $_POST['inkoopprijs'];
$verkoopprijs = $_POST['verkoopprijs'];
$last_modified = $_POST['last_modified'];
mysqli_query($db, "UPDATE prijzen SET inkoopprijs='$inkoopprijs', verkoopprijs='$verkoopprijs', last_modified= '$last_modified' WHERE id=$id");
$_SESSION['message'] = "price updated!";
header('location: C_R_U_D.php');
`
i have tried alerts, and even after i hit cancel it still did the editing
CodePudding user response:
You can add onclick
event handler to your Save
or Update
button. Once the button is clicked, call built-in confirm()
function that returns true
or false
depending on user's choice. In case of false
, you would like to prevent browser's default behavior and cancel form submission, which may be achieved by calling event.preventDefault()
.
<button type="submit" onclick="confirm('Are you sure?') || event.preventDefault()">Save</button>