简体   繁体   中英

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. The ID is in the href and send to the exact user 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.

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:

$('#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:

$(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:

$(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. check the documentation . $id_user is not defined and you are not using named parameter in your bindParam. try this:

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

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