簡體   English   中英

Ajax 表不顯示任何內容

[英]Ajax table doesn't show anything

我正在使用 ajax 和 php 創建一個表,但是有一個問題,該表沒有顯示在我的 div 中。 我對 ajax 真的很陌生,所以我還沒有完全理解它。

這是我的div:

<div class="body" id="live-data">
</div>

這是 ajax 代碼:

$(document).ready( function() {
    function fetch_data() {  
        $.ajax({  
            url:"fetch.php",  
            method:"POST",  
            success:function(data){  
                $('#live_data').html(data);  
            }  
        });  
    }
    fetch_data(); 
});

這是 fetch.php:

<?php

include('../global/db.php');

$output = ''; 
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";  
$result = mysqli_query($db, $sql);  
$output .= '  
<div class="table-responsive">  
    <table class="table table-bordered table-striped table-hover dataTable js-exportable">
        <thead>
            <tr>
                <th width="135" class="noExport">Action</th>
                <th width="90">LRN</th>
                <th width="20">Level</th>
                <th>Name</th>
                <th width="20">Gender</th>
                <th width="60">Type</th>
                <th width="105" style="font-size: 14px!important;">Date Enrolled</th>
            </tr>
        </thead>
        <tbody>';
if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_array($result)){ 
        $output .= ' 
            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;
                    <button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
                        <i class="material-icons">person</i>
                        <span>Parent</span>
                    </button>
                </td>
                <td><?php echo $stud_lrn ?></td>
                <td><?php echo $stud_grade ?></td>
                <td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
                <td><?php echo $stud_gender ?></td>
                <td><?php echo $stud_type ?></td>
                <td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
            </tr>
        ';
    }
}

else {  
        $output .= '
            <tr>  
                <td colspan="12">Data not Found</td>  
            </tr>';  
}  

 $output .= '
        </tbody>
    </table>  
</div>';  

echo $output;  

?>

如果有人可以提供幫助,那就太好了,因為我只是不知道為什么它不起作用

編輯:我已經更改了代碼,因此它在控制台選項卡中返回數據,這是顯示的內容:

<div class="table-responsive">  
    <table class="table table-bordered table-striped table-hover dataTable js-exportable">
        <thead>
            <tr>
                <th width="135" class="noExport">Action</th>
                <th width="90">LRN</th>
                <th width="20">Level</th>
                <th>Name</th>
                <th width="20">Gender</th>
                <th width="60">Type</th>
                <th width="105" style="font-size: 14px!important;">Date Enrolled</th>
            </tr>
        </thead>
        <tbody> 
            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;
                    <button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
                        <i class="material-icons">person</i>
                        <span>Parent</span>
                    </button>
                </td>
                <td><?php echo $stud_lrn ?></td>
                <td><?php echo $stud_grade ?></td>
                <td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
                <td><?php echo $stud_gender ?></td>
                <td><?php echo $stud_type ?></td>
                <td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
            </tr>

            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;
                    <button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
                        <i class="material-icons">person</i>
                        <span>Parent</span>
                    </button>
                </td>
                <td><?php echo $stud_lrn ?></td>
                <td><?php echo $stud_grade ?></td>
                <td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
                <td><?php echo $stud_gender ?></td>
                <td><?php echo $stud_type ?></td>
                <td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
            </tr>

            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;
                    <button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
                        <i class="material-icons">person</i>
                        <span>Parent</span>
                    </button>
                </td>
                <td><?php echo $stud_lrn ?></td>
                <td><?php echo $stud_grade ?></td>
                <td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
                <td><?php echo $stud_gender ?></td>
                <td><?php echo $stud_type ?></td>
                <td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
            </tr>

            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;
                    <button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
                        <i class="material-icons">person</i>
                        <span>Parent</span>
                    </button>
                </td>
                <td><?php echo $stud_lrn ?></td>
                <td><?php echo $stud_grade ?></td>
                <td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
                <td><?php echo $stud_gender ?></td>
                <td><?php echo $stud_type ?></td>
                <td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
            </tr>

        </tbody>
    </table>  
</div>

所以它清楚地返回了正確的數據,但它只是沒有出現在實時數據 div 中

也許看看你的 html 你的 div 在哪里。 您的 div id 顯示為“live-data”,並且在 ajax 代碼中您提到為 div id“#live_data”而不是“#live-data”獲取數據。 也許將它們更改為相同的名稱可以解決您的問題。 我將使用 thr div id 和 ajax 相同的 id 名稱,如“#liveData”。

示例(相同的代碼,只是編輯為正確的 ID,與您的原始代碼進行比較):

你的 HTML div

<div class="body" id="liveData">
</div>

您的 ajax 代碼

$(document).ready( function() {
    function fetch_data() {  
        $.ajax({  
            url:"fetch.php",  
            method:"POST",  
            success:function(data){  
                $('#liveData').html(data);  
            }  
        });  
    }
    fetch_data(); 
});

只是 append 成功的 div 標簽的正確 ID function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM