简体   繁体   English

在dataTable服务器端删除

[英]delete in dataTable server side

I have this issue in my users list, the dataTable server side is working nice, show me all the users I have I put the buttons for view profile, edit profile and delete profile. 我的用户列表中有这个问题,dataTable服务器端工作正常,向我显示所有用户,我放置了用于查看配置文件,编辑配置文件和删除配置文件的按钮。 The ID is in the href and send to the exact user id. 该ID位于href中,并发送给确切的用户ID。 The only one error is when I try to delete any user...the ajax I have to delete the user by ID don't do nothing... can you help me, please. 唯一的错误是当我尝试删除任何用户时...我必须通过ID删除用户的ajax不会执行任何操作...请您能帮我吗。

Here is the server side (only the connection): 这是服务器端(仅连接):

$aColumns = array( 'id_user', 'nombre', 'apellido', 'id_tipo');

/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id_user";

/* DB table to use */
$sTable = "USERS";

/* Database connection information */
$gaSql['user']       = "soft_chas";
$gaSql['password']   = "123456";
$gaSql['db']         = "soft_chas";
$gaSql['server']     = "localhost";

The js dataTable: js dataTable:

$('#userTabla').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>",
    "sAjaxSource": "server_processing.php",
    "aoColumns": [
        { "mData": "id_user" },
        { "mData": "nombre" },
        { "mData": "apellido" },
        { "mData": "tipo" },
        {
            "mData": null,
            "sClass": "center",
            "sDefaultContent": "",
            "fnRender": function (o) {
            return '<a href="user_profile.php?id_user=' + o.aData[0] + '" class="btn btn-success"><i class="icon-user icon-white"></i> Ver perfil</a> <a href="user_edit.php?id_user=' + o.aData[0] + '" class="btn btn-info"><i class="icon-edit icon-white"></i> Editar</a> <a id="' + o.aData[0] + '" class="btn btn-danger" href="#"><i class="icon-trash icon-white"></i> Borrar</a>'
        },
        "aTargets": [3]
        }
    ],
    "sPaginationType": "bootstrap",
    "oLanguage": {
    "sLengthMenu": "_MENU_ registros por pag"
    }
});

JS for delete user by ID: 通过ID删除用户的JS:

$(document).ready(function()
{
    $('table#userTabla td a.btn-danger').click(function(e)
    {
        if (confirm("<?php $translate->__("Do you really want to delete User's record?"); ?>"))
        {
         e.returnValue = false;
         var id = $(this).attr('id');
         var data = 'recordToDelete='+ id;
            var parent = $(this).parent().parent();
            $.ajax(
            {
                   type: "POST",
                   url: "include/delete.php",
                   data: data,
                   cache: false,

                   success: function()
                   {
                        parent.fadeOut('slow', function() {$(this).remove();});
                   }
             });                
        }
    });
    $('table#userTabla tr:odd').css('background',' #FFFFFF');
});

The DELETE code: 删除代码:

<?php 
include_once("configs.php");
if(isset($_POST["recordToDelete"]) && strlen($_POST["recordToDelete"])>0 && is_numeric($_POST["recordToDelete"])) {
  $idToDelete = filter_var($_POST["recordToDelete"], FILTER_SANITIZE_NUMBER_INT);
    if($stmt = $conn->prepare("DELETE FROM USERS WHERE id_user = $idToDelete"))
    $stmt->bindParam("$idToDelete", $id_user, PDO::PARAM_INT);
    $stmt->execute();
  }
$conn = null;
?>

The table in the page: 页面中的表:

<table class="table table-striped table-bordered bootstrap-datatable" id="userTabla" serverSide="true" processing="true">
        <thead>
            <tr>
            <th>ID</th>
            <th><?php $translate->__('Name'); ?></th>
            <th><?php $translate->__('Last Name'); ?></th>
            <th><?php $translate->__('Type'); ?></th>
            <th><?php $translate->__('Actions'); ?></th>
        </tr>
    </thead>   
    <tbody>
        <tr>
            <td colspan="5" class="dataTables_empty center"><i class="icon-refresh"></i><img src="img/ajax-loaders/ajax-loader-1.gif" title="ajax-loaders">&nbsp;<?php $translate->__('Please wait'); ?> ...</td>
        </tr>
    </tbody>
</table>

try with this ajax js: 试试这个ajax js:

$(document).ready(function(){
    $(document).delegate('.btn-danger', 'click', function() { 
        if (confirm("<?php $translate->__("Do you really want to delete User's record?"); ?>"))
        {
            var id = $(this).attr('id');
            var data = 'recordToDelete='+ id;
            var parent = $(this).parent().parent();
            $.ajax({
                type: "POST",
                url: "include/delete.php",
                data: data,
                cache: false,
                success: function()
                {
                    parent.fadeOut('slow', function() {$(this).remove();});
                }
            });                
        }
    });
    $('table#userTabla tr:odd').css('background',' #FFFFFF');
});

your bindParam statement got some issues. 您的bindParam语句出现了一些问题。 check the documentation . 检查文档 $id_user is not defined and you are not using named parameter in your bindParam. $ id_user未定义,并且您没有在bindParam中使用命名参数。 try this: 尝试这个:

if($stmt = $conn->prepare("DELETE FROM USERS WHERE id_user = ?"))
$stmt->bindParam(1, $idToDelete, PDO::PARAM_INT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM