javascript - Delete MYSQL DB Entry via PHP / HTML Button

Solution:

You have two ways:

1:
use GET method:

<a href="/delete.php?id='.$row['car_id'].'" class='btn btn-outline-danger text-white'>DELETE</a>

In the delete.php:

$id=$_GET['id'];

And query the delete.

2:
use ajax:

 <button value="'.$row['car_id'].'" class="btn btn-outline-danger text-white" onclick="Delete(this)">DELETE</button>

js:

   function Delete(elem) {
                var id= elem.value;
               
                      $.ajax({
                        url: "delete.php",
                        type: "POST",
                        data: {id: id},
                        success: function (data) {

                            alert('Done');
                            
                        }
                    });
                

            }

in delete.php:

 $id=$_POST['id'];

And query the delete.

Please improve all your query.(prepare stmt)

Answer

Solution:

You can use form with action button name to identify what part of action to do

    <?php
      
        if(isset($_POST['delete_rows'])) { //<-- see input name
            ... delete anything from database ...
        } else
        if(isset($_POST['select_rows'])) { //<-- see input name
            ... select anything from database ...
        } 
    ?> 
      
    <form method="post"> 
        <input type="submit" name="delete_rows" value="Delete"/>
        <input type="submit" name="select_rows" value="Select"/>
    </form>

Answer

Solution:

You can do it in two ways
1- you can make a hyperlink and pass row value to it like <a herf="page.php ? Id= $row['id']>del</>
2- you can make a button inside form method post and post the id and delete the record .

Note: in php it work with name tag not the id

I hope this answer help you

Source