简体   繁体   中英

jQuery delete row from SQL (button) not working

I have a table of notes on a page. I want the user to be able to add and delete notes dynamically. I have the add notes feature working, but I am having trouble with the delete notes feature. I can get the row to fade out on click of the delete button, but I cannot get the row to delete from the database. Here is my code:

HTML:

            <td class='deleteCell'>                                         
                <input id=$noteID type='submit' class='delBtn' value='Delete'>        
            </td>                                                           
            <td class='dateCell'>                                           
                <p class='dateText'> $noteDate </p>                         
            </td>                                                           
            <td class='noteCell'>                                           
                <p class='noteText'> $noteText </p>                         
            </td>                                                           
        </tr>                                                           

jQuery:

$(".delBtn").click(function() {
    var del_id = $(this).attr('id');
    var parent = $(this).parent().parent();
    $.post("deleteNote.php" , {id : del_id},
        function() {
            parent.fadeOut('slow',
                function() {
                    $(this).remove();
                }) ;
        }) ;
}) ;

deleteNote.php:

<?php
include( 'includes/sql_link.php' )  ;


if(isset($_POST['id'])) {
    $post  =   mysqli_real_escape_string ( $db , $_POST['id'] ) ;
    $query = " DELETE FROM Room_Notes WHERE Note_ID LIKE LIKE '{$post}%'";

    if(query($query)) {
        echo "YES";
    }
    else {
        echo "NO";
    }
}
else {
    echo "FAIL";
}

It removes the row from the table but not the database. everytime I refresh the page

Firstly you have two "LIKE"'s in your query.

 DELETE FROM Room_Notes WHERE Note_ID LIKE '{$post}%'

(Would this not be better as a = anyway?)

Let me know the outcome of the alert :)


Try changing your post request to this:

$.post( "deleteNote.php", { id: del_id } );

Alternatively you could change all POST parameters in deleteNote.php to GET parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM