Friday, 30 December 2022

delete an entry from the database on click of a button from the webpage in javascript/php

I have a html/php/javascript code as shown below which outputs the following 3 elements on the webpage (shown below the code):

index.php:

<html>
<head> ABCDE </head>
<body>
<dl id="dl_<?php echo $row['attribute']; ?>"><a href="abcde.php?<?php echo 'id='.rawurlencode($row['attribute'])
        .'&action='.rawurlencode($row['action'])
        .'&name='.rawurlencode($row['map_value_name']); ?>">
        <?php echo $row['attribute']; ?></a> (
    <?php echo $row['owner']; ?>)
    <button type="button" class="btn btn-danger"
        onclick="deleteAttribute('<?php echo $row['attribute']; ?>')">Delete</button>  <!-- Delete button -->
    <dd><?php echo $row['description']; ?></dd>
</dl>

<!-- script -->

<script>
    function deleteAttribute(attribute) {
        console.log(attribute);
        console.log('I am in attribute');
        jQuery.ajax({
            url: 'delete.php',
            type: 'post',
            data: 'attribute='+attribute,
            success: function(result) {
                jQuery('#dl_'+attribute).hide(500);
            }
        });
    }
</script>
</body>
</html>

It prints:

Good Morning
Hello World

Good Evening
World

Good Afternoon
Hello

I am using the following file to delete an entry from the database on click of a delete button from the webpage.

delete.php file

<?php
require 'connect.php';

$attribute = $_POST['attribute'];
$sql = "Delete from attributes where attribute='".$attribute."'";
$stmt = $con->prepare($sql);
$stmt->execute();

?>

Problem Statement:

On click of a delete button from the index.php file above, I don't see any entry getting deleted both from the webpage and the database.

I am wondering what changes I need to make in the delete.php and index.php files above so that on click of a Delete button, entry both from the webpage and the database gets deleted.



from delete an entry from the database on click of a button from the webpage in javascript/php

No comments:

Post a Comment