Jquery Ajax remove rows from table and in db

后端 未结 4 1159
暖寄归人
暖寄归人 2020-12-10 21:45

I have a html table which if filled by values of an mysql table. This way:

function getCategories(){
    $prod = new C_Product();
    $cat= $prod->getCate         


        
4条回答
  •  北海茫月
    2020-12-10 22:35

    Your intuitions about this problem were on the right track. You need to pass the database id of each record and store it somewhere in the HTML so that you know what value to pass to the backend. If you were planning to implement a solution that degrades gracefully for users without javascript (usually a good idea) what you might do is implement a non-ajax view that deletes a single row. If your application uses pretty URLs, that view might be accessed by /table/delete/23 (or whatever id). In you table generation code, you want to add a link for each row that looks like this:

    Delete
    

    At this point, you should have a nicely working non-javascript solution. Now we can add the AJAX. This script uses some javascript string mangling to extract the last part of the href and turn it into an id.

    $(".delete-button").click(function() {
        var id = this.href.slice(this.href.lastIndexOf('/')+1);
        // perform AJAX call using this id
        return false; // return false so that we don't follow the link!
    });
    

    Also, when you are implementing your backend PHP function to accept the AJAX request, don't forget to sanitize your input!

提交回复
热议问题