0

I am trying to create a dynamic link that will let the user delete a row from a mysql database based on the rows ID #.

I am also trying to input some javascript confirmation that the user really wants to delete the row in question.

My question is how to accomplish this? I have some ideas...(see below) am I on the right track?

<head>
    <?php
        require 'include/episodelist.db.php';
        mysql_query("DELETE FROM season WHERE ID='$id'");

        mysql_close();
    ?> 
    <script type="text/javascript">
        function confSubmit(form) {
            if (confirm("Delete this record?")) {
                form.submit();
            } else {
            }
        }
    </script>
</head>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
    <input type="button" onClick="confSubmit(this.form);" value="Delete">
</form>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
webmaster alex l
  • 663
  • 3
  • 16
  • 32
  • Assuming you have only 1 row per page/form which you'd like delete, it should work as long as you add a hidden input to submit the row #ID which you want to delete and process it in your PHP. Oh, don't forget to add the `event` parameter to your function, so you can call `event.preventDefault()` to prevent the form from submitting while the confirm dialog is open. – Fabrício Matté May 29 '12 at 03:20
  • The page has many items listed from the database. The admin can see a link next to each that says delete. Each item has its own ID in the mysql database that im trying to reference. – webmaster alex l May 29 '12 at 03:23
  • How about `form`s? There's only one form for the whole page or one form for each row? – Fabrício Matté May 29 '12 at 03:24
  • the page itself is created by a php script that lists each item with an edit button (this works correctly) and now I want to add a delete button. I guess I could add it to the (edit) page. I thought this would be simpler then having to go into the edit page just to delete the record in question. Also, iv never used the {event.preventDefault()} so im not sure where to add it into the javascript. – webmaster alex l May 29 '12 at 03:28
  • Oh never mind, your button isn't a `submit` so you won't need to call `event.preventDefault()`. :) But you still didn't answer if there's one form for each line or not. I'm assuming there's one form for each line, as you're passing `this.form` as a parameter. Check your HTML to see if there are multiple forms. – Fabrício Matté May 29 '12 at 03:33
  • yes, there is one form for each line – webmaster alex l May 29 '12 at 03:51
  • Don't use GET for actions like this, or prefetching browsers will ruin your day. When a link is clicked, you should show another form which uses POST to confirm deletion. – DCoder May 29 '12 at 04:29

1 Answers1

2

I can give you the basic idea how you can do this. Just create a link for each row which will redirect the control to delete.php(The file which will perform the deletion.) The control will only be redirect in case your user confirm the deletion operation. The link can be like this.

 <a class="button" href="<?php echo base_url();?>delete.php/<?php echo $row->id;?>" onclick="javascript: return confirm('Are you SURE you wish to do this?');">Delete</a>
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53