删除数据库中的数据而不重新加载页面

时间:2016-03-02 22:13:27

标签: javascript php jquery ajax

我想在不刷新页面的情况下从数据库中删除数据。我的代码正在运行但删除产品后需要刷新页面。我想要像this

这样的东西

这是我的js代码:

Bitmap bmp((INT)(reptileRect.Width * 2), (INT)(reptileRect.Width * 2));

和delete_product php代码:

<script>
    $(document).on('click', '.delete-it', function() {
        var id = $(this).attr('delete-id');
        bootbox.confirm("Are you sure?", function(result) {
            if (result) {
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "delete_product.php",
                    data: {
                        object_id: id
                    },
                    dataType: 'json',
                    success: function(data) {
                        location.reload();
                    }
                });
            }
        });
        return false;
    });
</script>

请告诉我一个制作它的方法!

1 个答案:

答案 0 :(得分:0)

我看不到你在网页上瞄准某些内容的地方,但这就是我要使用的地方:

<script>
function swapContent(href, url_data, target) {
    $.ajax({
        type: 'GET',
        cache: false,
        url: href+'?' + url_data,  //add a variable to the URL that will carry the value in your i counter through to the PHP page so it know's if this is new or additional data
        success: function (data) { // this param name was confusing, I have changed it to the "normal" name to make it clear that it contains the data returned from the request
            //load more data to "target" value div
            target.innerHTML = (data); // as above, data holds the result of the request, so all the data returned from your results.php file are in this param but please see below
        }
    })

}
    $(document).on('click', '.delete-it', function() {
        var id = $(this).attr('delete-id');
        bootbox.confirm("Are you sure?", function(result) {
            if (result) {
         swapContent(base_url, url_data, target) //set variables
   }
        });
        return false;
    });
</script>

注意:由于我使用了多少ajax,我自己保留了一个ajax函数