-2

I have a table with a delete <a> tag. I already can delete data. But what I want to do is put a yes or no warning if the user wants to really delete it. I try JavaScript but I can't make it work.

This is my code

    <?php
            $action = '';
            if(isset($_GET['action']))
            $action=$_GET['action'];
            if($action=='delete')
            {
               $speakerid=$_GET['speakerid'];
               $delete = mysqli_query($conn, "DELETE FROM accounts WHERE id=$speakerid");
               header("Location:accountList.php");
            }
        ?>

        <?php while($row = mysqli_fetch_array($search_result)):?>
                    <tr>
                        <td><?php echo $row['firstname'];?></td>
                        <td><?php echo $row['lastname'];?></td>
                        <td><?php echo $row['emailaddress'];?></td>
                        <td><?php echo $row['contactnumber'];?></td>
                        <td><?php echo $row['status'];?></td>
                        <td><?php echo $row['date_added'];?></td>
                        <td align='center'><?php echo "<a style='padding:6px; background-color:red; color:white; border-radius: 3px;' href='accountList.php?action=delete&speakerid=".$row['id']."'>delete</a>";?></td>
                    </tr>                                                                                                               
        <?php endwhile;?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Red
  • 133
  • 2
  • 14
  • 4
    Please add the javascript. I'd think you could use https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm. You also should parameterize that query, as is open to injections – chris85 Sep 04 '17 at 14:26
  • 3
    Possible duplicate of [**How to show a confirm message before delete?**](https://stackoverflow.com/questions/9139075/how-to-show-a-confirm-message-before-delete) or [**How add confirmation box in php before deleting?**](https://stackoverflow.com/questions/25617583/how-add-confirmation-box-in-php-before-deleting) – Nope Sep 04 '17 at 14:32

1 Answers1

1

One easy way to do it would be :

<?php echo "<a style='padding:6px; background-color:red; color:white; border-radius: 3px;' href='accountList.php?action=delete&speakerid=".$row['id']."' onclick='return confirm(\"Really delete ?\");'>delete</a>";?>
Calimero
  • 4,238
  • 1
  • 23
  • 34
  • thanks sir it work. i already try onclick but my mistake is i didnt put " \ " in (\"really delete?\"). What is the purpose of " \ ". – Red Sep 04 '17 at 14:38
  • 1
    Since you are already inside a php double-quoted (") string, and at the same time in an html attribute delimited by single quotes ('), you would need to reuse either to pass in an expected javascript string to the confirm() function, I opted for double quotes in that case, that's why you need to escape these with backslashes (\) so that php wouldn't mistake them for the main string delimiters. – Calimero Sep 04 '17 at 14:52
  • 1
    You can try removing the backslashes and see the effect - you'll end up with a php syntax error for sure. – Calimero Sep 04 '17 at 14:53