首页 > 解决方案 > 未使用 php ajax 在引导表上加载数据

问题描述

我正在创建一个简单的 crud 系统,而我加载数据时未加载数据。但我成功地从 all_category.php 中获取了数据。我通过 console.log 检查了

0: {id: 65, catname: "sad", status: 1}
1: {id: 62, catname: "Pepsi", status: 1}
2: {id: 60, catname: "Mobile", status: 1} 

到目前为止,我尝试了什么,我在下面附上了代码。我尝试了 2 天,但无法解决这个问题。数据没有传递到表。我附上了下面的屏幕截图

图片

表的形式设计

   <table id="tblCenters" data-toggle="table" data-show-refresh="true"
                       data-show-toggle="true" data-show-columns="true" data-search="true"
                       data-select-item-name="toolbar1" data-pagination="true" data-sort-name="aid"
                       data-sort-order="desc">
                    <thead>
                    <tr>

                        <th data-field="catname" data-sortable="true">Category</th>
                        <th data-field="status" data-sortable="true">Status</th>
                        <th data-field="opt" data-sortable="true">Edit</th>
                    </tr>
                    </thead>

                    <tr>

                    </tr>

                </table>

jQuery

function get_all() {
        $.ajax({
            type: 'POST',
            url: 'all_category.php',
            dataType: 'json',

            success: function (data) {

                console.log(data);
                $('#tblCenters').bootstrapTable('removeAll');
                for (var i = 0; i < data.length; i++) {
                    var btnCell = '<button id="u' + data[i][0] + '" type="button" class="btn btn-success btn-xs"   onclick="updateAcc(this)">Edit</button>';
                    addRow(data[i][1], data[i][2], btnCell);
                }

            }
            ,
            error: function (data) {
                console.log(data);
            }
        });

        function addRow(catname,status,cell) {
            $('#tblCenters').bootstrapTable('insertRow', {
                index: 1,
                row: {
                    catname: catname,
                    status:status,
                    opt:cell

                }
            });
        }

all_category.php

<?php
include("db.php");

$stmt = $conn->prepare("select id,catname,status from category order by id DESC ");
$stmt->bind_result($id,$catname,$status);

if ($stmt->execute()) {
    while ( $stmt->fetch() ) {
        $output[] = array ("id"=>$id, "catname"=>$catname,"status"=>$status);
    }

    echo json_encode( $output );
}
$stmt->close();

标签: phpajax

解决方案


推荐阅读