【问题标题】:JQuery data table does not refresh after inserting dataJQuery数据表插入数据后不刷新
【发布时间】:2016-12-16 17:26:41
【问题描述】:

您好,我正在尝试从 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";
}

?>

【问题讨论】:

标签: javascript php jquery mysqli datatable


【解决方案1】:

试试这个:

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

而不是:

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

【讨论】:

  • 它确实会重新加载表,但会给出错误“无效的 json 响应”
  • 我应该更改我的 php 代码吗?我将我的 php 代码发布到
猜你喜欢
  • 2012-07-26
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多