My PHP page has a link to delete one MySQL table datum. I want it to be in such a way that when I click the \'delete\' link a confirmation box should appear and it should ask \
onclick="return confirm('Log Out?')? window.open('?user=logout','_self'): void(0);"
Did this script and afterwards i tought i go check the internet too.
Yes this is an old threat but as there seems not to be any similar version i thought i'd contribute.
Easiest & simplest way works on all browsers with/in any element or field.
You can change window.open
to any other command to run when confirmation is TRUE
, same with void(0)
if conformation is NULL
or canceled.
I was also looking for a way to do it and figured it out like this using forms and the formaction attribute:
<input type="submit" name="del_something" formaction="<addresstothispage>" value="delete" />
<?php if(isset($_POST['del_something'])) print '<div>Are you sure? <input type="submit" name="del_confirm" value="yes!" formaction="action.php" />
<input type="submit" name="del_no" value="no!" formaction="<addresstothispage>" />';?>
action.php would check for isset($_POST['del_confirm']) and call the corresponding php script (for database actions or whatever). Voilà, no javascript needed. Using the formaction attribute, the delete button can be part of any form and still call a different form action (such as refer back to the same page, but with the button set).
If the button was pressed, the confirm buttons will show.
<a onclick="javascript:return confirm('Are You Confirm Deletion');" href="delete_customer.php?a=<?php echo $row['id']; ?>" class="btn btn-danger a-btn-slide-text" style="color: white; width:86px; height:37px;" > <span class="glyphicon glyphicon-remove" aria-hidden="true"></span><span><strong></a></strong></span>
You should always use a form/post button to confirm something like this. Never rely on Javascript. Read this to understand why!
You can use JavaScript to prompt you:
Found this here - Example
<script>
function confirmDelete(delUrl) {
if (confirm("Are you sure you want to delete")) {
document.location = delUrl;
}
}
</script>
<a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>
Another way
<a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
Warning: This JavaScript will not stop the records from being deleted if they just navigate to the final url - delete.page?id=1 in their browser
Try this:
<td>
<a onclick="return confirm(\'Are you sure?\');" href="'.base_url('category/delete_category/'.$row->category_id).'" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Delete Coupon</a>
</td>