weixin_33725272 2016-12-30 05:07 采纳率: 0%
浏览 116

AJAX删除数据库条目

I am new to AJAX (and website development in general) and I am having trouble deleting a database entry in wordpress using jQuery and AJAX.
The user selects an entry in a table and clicks a button to delete it. The table is created using datatables plugin and extensive coding to make it work the way I need it to.
the jQuery code is in functions.php and the actual query is in a file on the server.
I have been using this how to delete records from database with an Ajax as a baseline for the code. Also, note that I am using wordpress My problem is that it does not seem to delete the entry from the database.

Here is the code in functions.php:

var GroupID2Del = dt.row({
        selected: true
    })
    .data()[23];
if (confirm('Are you sure you want to delete this row?')) {
    jQuery.ajax({
        type: 'POST',
        url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
        data: 'groupid=' + GroupID2Del,
        success: function(data) {
            dt.row({
                    selected: true
                })
                .remove();
            jQuery('#UserTable')
                .DataTable()
                .draw();
            alert(data);
        }
    });
}

GroupID2Del is how I am getting the id for it the selected row in the table. I am then removing the row from the table itself. The row does get removed.

Here is the ViewTable_DeleteEntry.php

<?php
    $dataPosted = $_POST['groupid'];
    $sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id =' . $dataPosted);
    mysql_query($sql);
    $count = mysql_affected_rows();
    print $count; 
?>

alert in function.php provides the correct sql, but the $count is empty since it does not actually delete anything.

  • 写回答

4条回答 默认 最新

  • weixin_33744141 2016-12-30 05:26
    关注

    Please Try This:

     var myKeyVals = { groupid : GroupID2Del}
        jQuery.ajax(
          {
             type: 'POST',
             url: '/wp-content/AJAX/ViewTable_DeleteEntry.php',
             data: myKeyVals,
             success: function(data)
             {
                dt.row({selected: true }).remove();                                             
                jQuery('#UserTable').DataTable().draw();
                alert(data);
             }
         });
    

    AND

    $dataPosted = $_POST['groupid'];
    if($dataPosted != ''){
    $sql = ('delete from wp_piic_formmaker_submits WHERE form_id = 13 and group_id ='.$dataPosted);
        mysql_query($sql); 
    
    $count = mysql_affected_rows();
    } else {
    echo 'Blank Record';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作