Hey i'm trying to make use of onclick =return confirm but whenever I click on the button, no prompt appears and it just heads to the redirect. My front end development isn't really the best. Any help is appreciated. Thanks
<td><a href="delete.php?id='.$row["userID"].'" onclick="return confirm("Are you sure you want to delete ?")" class="waves-effect waves-light btn-small red lighten-1"><i class="material-icons">delete</i></a></td>
My Answer:
It looks like you are using an anchor tag `` for the delete button. The issue might be that the anchor tag is redirecting to the `delete.php` page before the `confirm` dialog can appear.
To fix this issue, you can prevent the default behavior of the anchor tag by adding `event.preventDefault()` to the `onclick` event. Here's an updated version of your code:
html
delete
By adding `if(!confirm('Are you sure you want to delete ?')){event.preventDefault();}` to the `onclick` event, the anchor tag will only redirect to `delete.php` if the user confirms the delete action. Otherwise, the default behavior of the anchor tag will be prevented.
Rate this post
3 of 5 based on 1615 votesComments