![](/img/trans.png)
[英]Succeeded in fetching data, but it gives TypeError: Cannot read property '0' of null
[英]Cannot read property childnotes of null while fetching data with ajax in php
您好,我在使用 ajax 获取数据时遇到错误,因为无法读取 null 的属性 'childNodes' 任何人都可以告诉我我的代码出了什么问题,实际上我已经制作了一个表格来搜索数据,日期从至今获取记录我想在表中显示它们但 childnode 错误显示我如何解决它。 最有趣的是,当我控制台时,我找到了所需的数据,只是它没有显示在 div2 内部。
查询
$(document).on('submit','#date-search',function(e){
e.preventDefault();
// var url = $(this).attr('action');
var data = $(this).serialize();
$.ajax({
url:"model/orders/orders.php?selectdate",
type:"POST",
data:data,
success:function(data){
// console.log(data);
// $('#date-search')[0].reset();
$('#div2').html(data);
if(data==''){
$('#div2').append('<tr class="odd"><td valign="top" colspan="5" class="dataTables_empty">No data available in table</td></tr>');
}
// $("#div2").html(data, function () {
// $(".table").dataTable({
// retrieve:true,
// });
// });
}
});
return false;
});
php部分
if(isset($_GET['selectdate'])){
$date_from = $_POST['date_from'];
$date_to = $_POST['date_to'];
$sql_date = "select * from $tb_orders where book_date >= '$date_from' and book_date <= '$date_to' order by id DESC";
$res_date = mysqli_query($conn,$sql_date);
if($num_date = mysqli_num_rows($res_date) > 0){
$i=0;
while($rows_date = mysqli_fetch_assoc($res_date)){
$i++;
$order_id = $rows_date['id'];
$product = $rows_date['product_id'];
$total_price = $rows_date['total_price'];
$transac = $rows_date['transaction_no'];
$sel_pro = "select * from $tb_products where id='$product'";
$res_pro = mysqli_query($conn,$sel_pro);
if($res_pro){
$rows_pro = mysqli_fetch_assoc($res_pro);
$pname = $rows_pro['pname'];
}
$sel_ro = "select * from $tb_customers where order_id='$order_id'";
$res_ro = mysqli_query($conn,$sel_ro);
if($res_ro){
$rows_ro = mysqli_fetch_assoc($res_ro);
$cust_name = $rows_ro['cust_name'];
$cust_number = $rows_ro['cust_number'];
}
?>
<tr>
<td><?php echo $i; ?></td>
<td>
<?php
if($cust_name == ''){
echo 'N/A';
}else{
echo $cust_name;
}
?>
</td>
<td><?php echo date("d-m-Y", strtotime($rows_date['book_date'])); ?></td>
<td><?php echo ucfirst($pname); ?></td>
<td><a href="../uploads/<?php echo $rows['file']; ?>" target="_blank" class="btn btn-success btn-sm ml-1" download>Download</a></td>
<td>Rs. <?php echo $total_price; ?></td>
<td>
<?php
if($transac == ''){
echo 'N/A';
}else{
echo $transac;
}
?>
</td>
<td>
<button onclick="alertify.confirm('Want to Delete','Are you sure you want to Delete?',function(){typedel(<?php echo $rows['id']; ?>)},function(){});" class="btn btn-danger btn-sm ml-2" title="Delete"><i class="mdi mdi-delete"></i></button>
</td>
</tr>
<?php
}
}
}
html部分
<div class="card-body">
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<div class="mx-auto">
<form class="form-inline mt-4 d-flex justify-content-center" method="post" id="date-search">
<input type="date" class="form-control mb-2 mr-sm-2" id="date_from" name="date_from" placeholder="Date from" required>
<input type="date" class="form-control mb-2 mr-sm-2" id="date_to" name="date_to" placeholder="Date to" required>
<button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>
</div>
</div>
<div class="col-sm-2"></div>
</div>
</div>
<div class="table-responsive pl-3 pr-3" id="tableWrap">
<table class="table download-excel">
<thead class="thead-light">
<tr>
<th scope="col">#</th>
<th scope="col">Customer</th>
<th scope="col">Booking</th>
<th scope="col">Product</th>
<th scope="col">File</th>
<th scope="col">Amount</th>
<th scope="col">Transaction Details</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody id="div2">
</tbody>
</table>
</div>
我认为您可能在 php 中有拼写错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.