簡體   English   中英

如何使用PHP檢索MySQL中的表行並使用Javascript顯示它?

[英]How to retrieve table rows in MySQL using PHP and Display it with Javascript?

我正在嘗試使用MYSQL中的數據庫表加載並在html的數據表中顯示它。 但由於某些原因,即使我在數據庫中測試我的查詢,也沒有檢索到任何數據。 有人可以幫我解決這個問題嗎? 見下面的代碼:

 function loadAccountsList() { $.ajax({ type: 'POST', url: '../back_php_Code/pAdminList.php', dataType: 'json', contentType: 'application/json; charset=utf-8', success: function(response) { $('#AccountList').empty(); var cells = eval("(" + response + ")"); alert(cells); for (var i = 0; i < cells.length; i++) { $('#AccountList').append('<tr data-empid="' + cells[i].Code + '">' + '<td style="font-size:11px; text-align:center;">' + cells[i].name + '</td>' + '<td style="font-size:11px; text-align:center;">' + cells[i].typeofloan + '</td>' + '<td style="font-size:11px; text-align:center;">' + cells[i].bank + '</td>' + '<td style="font-size:11px; text-align:center;">' + cells[i].amount + '</td>' + '<td style="font-size:11px; text-align:center;">' + cells[i].status + '</td>' + '</tr>'); } }, error: function(error) { console.log(error); } }); } 
 <script type="text/javascript" src="..\\jsScripts\\jsAdminList.js"></script> <div class="card"> <div class="card-block"> <div class="dt-responsive table-responsive"> <table id="alt-pg-dt" class="table table-striped table-bordered nowrap"> <thead> <tr> <th>CI_CODE</th> <th>Fullname</th> <th>Type of Loan</th> <th>Bank</th> <th>Amount</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody id="AccountList"></tbody> <tfoot> <tr> <th>CI_CODE</th> <th>Fullname</th> <th>Type of Loan</th> <th>Bank</th> <th>Amount</th> <th>Status</th> <th>Action</th> </tr> </tfoot> </table> </div> </div> </div> 


謝謝並恭祝安康,

在此輸入圖像描述

我認為你錯誤地成功了。 您不需要在響應中添加引號並分配給單元格變量

 success: function (response) {
        // Now your `response` is in json which which is an array of objects
        // emptying table which is fine
        $('#AccountList').empty();
        // you are using jquery so you can simply iterate through queries like this
        $(response).each(function(index, row){
          $('#AccountList').append('<tr data-empid="' + row.empID + '">'
                + '<td style="font-size:11px; text-align:center;">' + row.badgenum + '</td>'
                + '<td style="font-size:11px; text-align:center;">' + row.empName + '</td>'
                + '<td style="font-size:11px; text-align:center;">' + row.groupName + '</td>'
                + '<td style="font-size:11px; text-align:center;">' + row.email + '</td>'
                + '<td style="font-size:11px; text-align:center;">' + row.contact_no + '</td>'
                + '<td style="font-size:11px; text-align:center;">' + row.empStatus + '</td>'
            + '</tr>');

        });

    },

暫無
暫無

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

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