View Full Version : Confirmation box and PHP

Feb 2nd, 2010, 02:37 AM
I have an action in PHP where, when a user clicks the delete button i want a confirmation box. How does a confirmation box work with a PHP function. Here is the function.

<a href="/order/delete/id/<?php echo $o->id?>"><img src="/gfx/trashicon.gif" /></a>

Any help would be appreciated!

Feb 2nd, 2010, 03:08 AM
So you want the user to click the link, which will direct them to '/order/delete/id/100' for example, and a confirmation dialog will pop up, to confirm they want to delete that item / record, correct?

I'd use a DOM method to grab that anchor and attach an onclick event like so:

<a href="/order/delete/id/<?php echo $o->id?>" class="deleteRecord">
<img src="/gfx/trashicon.gif" />

<script type="text/javascript">
// grab all anchor tags
var allLinks= document.getElementsByTagName("a");
// loop through them
for ( var i=0, l= allLinks.length; i<l; i++ ) {
// filter out all the links that have the 'deleteRecord' class
if ( allLinks[i].className == "deleteRecord" ) {
// attach event handler
allLinks[i].onclick= function(clickEvt){
// return true or false based on the user's choice
// true - follow the link
// false - do nothing
return confirm("Are you sure you want to delete this record?");

Note this SCRIPT element can be an externally linked script that is called on page load, or inserted near the bottom of the BODY passed all the rendered elements.

If the user selects "OK", then the link is followed and the PHP target script on the other side of that link will load and process the id value, thus deleting the record. If they select "Cancel", then the function returns false and the link is not followed. PHP has nothing to do with it, really, but to sit and wait for a request to come along.

The implementation I'm suggesting uses "unobtrusive JavaScript" to allow those users that do not have JS enabled or are using older browsers to still view a functional page. Those that do have JS enabled will see the confirmation dialog. If you need to force the confirmation, I'd have some logic in your PHP script that asks for confirmation as a backup. Maybe set a cookie in PHP that is deleted by JavaScript. If the cookie exists when the user gets to that script, force a confirmation on the server side. If not, then assume the JS already performed the operation. Hacky, but better than nothing.

Feb 2nd, 2010, 03:22 AM
It doesnt seem to be working, i may be doing something wrong. I have exactly what you have down. It just deletes the flyer like it is suppose to.

Feb 2nd, 2010, 03:24 AM
Define "quite a few". 100? 1000?

If you push the script element down to the bottom of your body just before the closing body tag, it will execute after the page has loaded and rendered. I doubt you'll even notice it.

Feb 2nd, 2010, 03:44 AM
When i moved it down, it worked. Thank you very much!!!

Feb 2nd, 2010, 03:47 AM
When i moved it down, it worked. Thank you very much!!!


Remember, any script that references the DOM has to be either at the bottom of the body, or it can be externally linked or in the document head, but it has to be called with the window.onload event to work. Otherwise, it just goes full steam and tries to reference DOM nodes that the document hasn't created yet.

Feb 2nd, 2010, 03:55 AM
Ha! That explains soooo much. You have no idea.