Javascript confirmation box for deleting rows with PHP

Here’s a quick handy bit of code. I was looking for a way to delete records from a database with PHP on a per-row basis via a custom administration area, just with a simple standard "delete" link. Ideally, I wanted this with a no-refresh thing going on, and a confirmation box to say "are you sure …" but that would require a bit of Ajax which I didn’t want to install on the site I was working on just for that reason.

So, here’s what I did

From somewhere else on the ‘net I grabbed this code, and put this in the HEAD section. This code will create a standard confirmation box asking if you’re sure if you want to delete this. Clicking No will close the box, clicking Yes will redirect to wherever you want.

<script language="javascript1.2">
var form_id;
function confirm_delete(go_url)
{
var answer = confirm("Are you sure to delete the  selected row?");
if (answer)
{
location=go_url;
}
}
</script>

Then, for the delete link, simply use this code:

<a href="#" onClick="confirm_delete('page.php?delete=true&id=5');">delete</a>

Of course, on my page I replaced the id=5 with dynamic PHP code to echo out the ID of whatever row or record I was referring to.

Spot on, nice and quick too!

 

3 comments about Javascript confirmation box for deleting rows with PHP

  1. Tim says:

    Thanks Daze. That was really helpful. If slightly superfluous for Inn on the Beach.

  2. David says:

    Thank you !!! This really helped me alot. This is exactly what i wanted to do for my database. And i kept searching for some houres and trying to fix it on my own. But nothing worked. Until finally i found your easy but so cool peace of code :-)

    Greetings from Germany

  3. Daze says:

    No problem, David, glad you found it useful. I have another “version” which I am yet to post on my site, but keep an eye out. Even smaller, quicker code, and easier to implement.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>