简体   繁体   中英

JQuery / AJAX delete item from database

I'm trying to make a CRUD in jquery/ajax/php for learning purposes. But I can't figure out what I'm doing wrong with the delete part.

My goal is to delete an record from the database without refreshing the page.

Ajax function:

    $(document).on('click', '.deleteOrder', function(e){
    var id = $(this).attr('id');
    console.log('Clicked order: ' + id);

    $.ajax({
        type:   'POST',
        url:    'orders/deleteorder/',
        data:   {
            orderId: id
        },
        success: function(data){
            updateOrder(e);
        },
        error: function(){
            console.log('error');
        }
    });
});

Php function:

    public function deleteOrder(){
    $orderId = $_POST['id'];
    $count=$this->connection->prepare("DELETE FROM orders WHERE orderNumber = :number");
    $count->bindParam(":number",$orderId,PDO::PARAM_INT);
    $count->execute();
    echo 'Finished order ' . $orderId;
}

首先在浏览器的“网络”选项卡中检查是否通过ajax调用了删除功能(单击浏览器中的F12按钮时会看到此功能)

您没有调用deleteOrder函数。

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