Search code examples
javascriptjqueryajaxreload

Reload the content in Ajax function


I am trying to make a delete function with Ajax.

function delCatItem(catitem){
      var theitem = catitem;
            $.ajax({
        url: "movie/deleteitem/",
        type: "POST",
        data: { "movieid" : catitem },
        dataType: "html",
        success: 
        });
          }

The function takes an id number from a button and posts it to a model where it finds that item and deletes it from the database. This works. But, I am not sure what to put in the success part so that the content reloads without that item. Thanks!

EDIT

<table class="table">
    <?php foreach ($movies as $movie){?>
    <tr>
    <td><button id="item_<?=$movie['movieid'];?>" onClick="delCatItem('<?=$movie['movieid']?>')"class="deletebut">
    <span class="glyphicon glyphicon-remove"></span></button>
    <td><h4><?=$movie['title'];?></h4></td>
    <td><img src="<?=$movie['thumburl'];?>"/></td>
</tr>
<?php } ?>
</table>

Solution

  • You don't need to reload the content, you need to remove the deleted item from the DOM. Without more info about your HTML, I can't be very specific.

    function delCatItem(catitem){
        var theitem = catitem;
        $.ajax({
            url: "movie/deleteitem/",
            type: "POST",
            data: { "movieid" : catitem },
            dataType: "html",
            success: function(){
               var selector = '#item_'+catitem;
               $(selector).parents('tr').remove();
            }
       });
    }