NoReceipt4Panda NoReceipt4Panda - 5 months ago 10
Ajax Question

How to delete file in MySQL Database and on screen using AJAX and PHP through the use of ID only?

Here's what the visual of my site looks like at the moment:

Delete-Button
The goal is for the delete button to not only delete from the database but also from the screen immediately after pressing it.

PHP code for the view/HTML:

<?php
$sth = $db->prepare("SELECT id, first_name, last_name FROM wlist");
$sth->execute();

$results = $sth->fetchAll();

foreach ($results as $result) {
echo "<tr><td>".$result['first_name']."</td><td>".$result['last_name']."</td><td><button class=\"delete_class\" id=\"".$result['id']."\" >DELETE</button></td></tr>";
}
?>


The bottom of this PHP page, is this javascript f(x):

<script>
$(".delete_class").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'delete_page.php',
data: {delete_id : del_id},
success: function (data) {
if (data) {
tx.executeSql('DELETE from MYDBTABLE WHERE id = ?', [delete_id], success, error);
}
else { // DO SOMETHING ON FAILURE
}
}
});
});
</script>


The problem with the above code is that success always comes back regardless of outcome. This and I'm a little confused on the correct way to thoroughly delete only that parent attribute of the row/table

I have the "delete_page.php" in the same folder as the PHP file containing all the above code. This file has this code:

<?php
$id = $_POST['delete_id'];
$query = "DELETE from MYDBTABLE where ID = $id";
?>


I'm looking for the most efficient way possible to format both my delete.php file as well as the ajax function.

Answer

A simple solution to it is, You should make a function lets say activate(). This function would be responsible to make a call to your server and get all the records from database to the client and populate the list or the table whatever you are using to display.

The delete button which you are placing should obviously be calling a function with the ID as a parameter. So the delete(id) should be another function in your script which you will call each time you delete a record.

The twist comes here. There are 2 main ways to delete the record from view.

1) (I PREFER THIS) As the data to be fetched from the database would not be that massive, You should call the activate() function from the success of delete(id) function's AJAX call. This will again request to the server bringing up the updated Database.

2) Another way to do it is, As we populate the table in html with a large array, array.splice() function can be used which can delete any array element only by its index. So this should be done in the success of delete(id) function. This should be used in case of more traffic over server or larger data to be transferred from server to the client.

This could be the php script for delete

<?php
    $id = $_GET['delete_id'];
    $query = "DELETE from TABLENANE where ID='$id'";
    mysqli_query($conn,$query) or die(mysqli_error($conn));
    echo true;
?>

This could be javascript delete function

function delete(del_id){
$.ajax({
        url: "delete.php?delete_id=" + del_id,
        cache: false,
        success: function (result) {
           activate();                
       }
    });
}

this could be get query from database.

<?php

$result = mysqli_query($conn,"SELECT * FROM TABLENAME") or die(mysqli_error($conn));
$ara = array();
while($x = mysqli_fetch_assoc($result)){
     array_push($ara,$x);
}

echo json_encode($ara);
?>

And this could be activate function

var myData;
function activate(){
$.ajax({
        url: "getData.php",
        cache: false,
        success: function (result) {
            myData = result;
        }
    });
}
Comments