gpt4 book ai didi

javascript - JQuery数据表插入数据后不刷新

转载 作者:行者123 更新时间:2023-12-03 06:21:07 25 4
gpt4 key购买 nike

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

?>

最佳答案

试试这个:

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

而不是:

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

关于javascript - JQuery数据表插入数据后不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com