Search code examples
javascripthtmlformsconfirmation

Delete confirmation for html form


I would like to add a delete confirmation to my form button Possible javascript to use??

<script type="text/javascript"> 
function confirmDelete() { 
return confirm("Are you sure you want to delete?");   
} 
</script> 

This is currently working inside my form.

<input type="button" onclick="parent.location='<?php echo "delete.php?id=" . $people['id'] ?>'" value='Delete'>

How can I integrate the javascript into the onclick event? Either using the above javascript or any other way that may be better


Solution

  • Bind your event handler in javascript.

    <input type="button" id="delete" value='Delete' />
    
    document.getElementById('delete').onclick = function () {
        if (confirm('Are you sure?')) {
            parent.location='<?php echo "delete.php?id=" . $people['id'] ?>';
        }
    };