插入数据后,JQuery数据表不会刷新

时间:2016-08-10 11:00:25

标签: javascript php jquery mysqli datatable

您好我正在尝试插入和删除Jquery数据表中的数据

$(document).ready(function() {
            $('#example').dataTable( {
                 "aProcessing": true,
                 "aServerSide": true,
                 "destroy": true,
            } );
});

$(document).on('click','#btn_add',function(){
            var machine_id = $('#machine_id').text();
            var serial_no = $('#serial_no').text();
            var model = $('#model').text();
            var price = $('#price').text();
            var spare_parts = $('#spare_parts').text();
            var location = $('#location').text();
            if(machine_id == '')
            {
                alert("insert machine id");
            }
            $.ajax({
                url:"insert.php",
                type: "POST",
                data:{'machine_id':machine_id, 'serial_no':serial_no, 'model':model, 'price':price, 'spare_parts':spare_parts, 'location':location},
                dataType:"text",
                success:function(data){
                    alert(data);
                    $("#example").dataTable().fnDraw();
                }

            });     
        });

我在单击添加按钮时调用上面的代码,它将数据插入数据库但我的表不会自动刷新。请帮忙

我的PHP代码是

<?php    
$conn = mysqli_connect('localhost','root','','swastik_service');

$machine_id = isset($_POST["machine_id"])?$_POST['machine_id']:"";
$serial_no = isset($_POST["serial_no"])?$_POST['serial_no']:"";
$model= isset($_POST["model"])?$_POST['model']:"";
$price = isset($_POST["price"])?$_POST['price']:"";
$spare_parts = isset($_POST["spare_parts"])?$_POST['spare_parts']:"";
$location = isset($_POST["location"])?$_POST['location']:"";
$sql = "INSERT INTO `machine`(`machine_id`, `serial_no`, `model`, `price`, `spare_parts`, `location`)VALUES ('$machine_id', '$serial_no', '$model','$price','$spare_parts','$location')";
if(mysqli_query($conn,$sql)){
    echo "Data Inserted";
}

&GT;

1 个答案:

答案 0 :(得分:1)

试试这个:

$('#example').DataTable().ajax.reload();

而不是:

$("#example").dataTable().fnDraw();