Sample Use of JavaScript Confirm Pop Up Boxes

Photo of Mike Dalisay
Modified Friday, October 8, 2010
by - @ninjazhai
I'm gonna give you an example on how to use JavaScript confirm pop up boxes to delete a database record.

JavaScript Confirm Pop Up Box

File Name: index.php

        <title>Sample Use of JavaScript Confirm Pop Up Boxes</title>
        <script type='text/javascript'>
            function delete_user( id ){
                var answer = confirm('Are you sure you want to delete this record?');
                if ( answer ){ //if user clicked ok
                    window.location = 'index.php?action=delete&id=' + id;
                } //you can add else here
    include 'config_open_db.php'; //for database connection
        $sql = "DELETE FROM users WHERE id = {$_REQUEST['id']}";
        mysql_query ( $sql ) or die('Database Error: ' . mysql_error());
        echo "User Deleted!";

   <a href='#' onclick="delete_user( 3 );">Delete</a>

Here's how it works:

1. When you run this code, you will see a delete link.

2. After clicking that link, a JavaScript Pop Up Box will appear saying ""Are you sure you want to delete this record?".

3. If you click on "Cancel", nothing will happen aside from the box will disappear.

4. If you click on "OK", the page will be redirected to itself together with the parameters "action" and "id", it is on this part window.location = 'index.php?action=delete&id=' + id;

5. It will then evaluate our PHP script to delete the record from the database.

For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.