javascript - 如何使用 ajax 和 php 填充表格并在表格行中添加按钮?
问题描述
我正在尝试从数据库中读取信息并将它们放在一个表中,并在每行的最后一列中包含按钮。除了添加按钮之外,我在让它工作时遇到了麻烦。
这是我的retrieve.php
<?php
require_once 'connect.php';
$output = array('data' => array());
$sql = "SELECT * FROM student_acc";
$query = $connect->query($sql);
$x = 1;
while ($row = $query->fetch_assoc()) {
$actionButton = '<a href="#" class="view" title="View" data-toggle="tooltip" onclick="viewAd('[ad_id]')"><i class="material-icons"></i></a>' +
'<a href="#" class="edit" title="Edit" data-toggle="tooltip" onclick="editAd('[ad_id]')"><i class="material-icons"></i></a>' +
'<a href="#" class="delete" title="Delete" data-toggle="tooltip" onclick="deleteAd('[ad_id]')"><i class="material-icons"></i></a>'
; // buttions I'd like to include
$output['data'][] = array(
$x,
$row['address'],
$row['single_unit'],
$row['single_rent'],
$row['sharing_unit'],
$row['sharing_rent'],
$row['date'],
$actionButton; //I'm not sure how to include this
);
$x++;
}
$connect->close();
echo json_encode($output);
我的 ajax.js
$(document).ready(function(){
$.ajax({
type: "POST",
dataType: "json",
url: "tableretrieve/retrievestudent.php",
data: {action: "load"},
success: function (response){
console.log(response);
$.each(response, function(index, obj) {
var row = $('<tr>');
row.append('<td>' + obj.address + '</td>');
row.append('<td>' + obj.single_unit + '</td>');
row.append('<td>' + obj.single_rent + '</td>');
row.append('<td>' + obj.sharing_unit + '</td>');
row.append('<td>' + obj.sharing_rent + '</td>');
row.append('<td>' + obj.date + '</td>');
row.append('<td>' + *Code to include buttions here* + '</td>');
$('table').append(row)
});
}
});
});
我用下面的代码做了一些测试,结果很好:
$(document).ready(function(){
var ActionButions ='<a href="#" class="view" title="View" data-toggle="tooltip"><i class="material-icons"></i></a>' +
'<a href="#" class="edit" title="Edit" data-toggle="tooltip"><i class="material-icons"></i></a>' +
'<a href="#" class="delete" title="Delete" data-toggle="tooltip"><i class="material-icons"></i></a>';
var row = $('<tr>');
row.append('<td>' + "Vanderbijlpark, Bowker str, CE4, 12" + '</td>');
row.append('<td>' + "3" + '</td>');
row.append('<td>' + "1500" + '</td>');
row.append('<td>' + "1" + '</td>');
row.append('<td>' + "2500" + '</td>');
row.append('<td>' + "2019/08/14" + '</td>');
row.append('<td>' + ActionButions + '</td>');
$('table').append(row);
});
我试图通过从数据库中读取值来复制上面的代码。
解决方案
该字符串应与 . (点),而不是 PHP 脚本中的 +(加号)。这里的 [ad_id] 是什么?如果它是数据库表的 id 列,请使用
.$row['ad_id'].
.$actionButton = '<a href="#" class="view" title="View" data-toggle="tooltip" onclick="viewAd('.$row['ad_id'].')"><i class="material-icons"></i></a>' . '<a href="#" class="edit" title="Edit" data-toggle="tooltip" onclick="editAd('.$row['ad_id'].')"><i class="material-icons"></i></a>' . '<a href="#" class="delete" title="Delete" data-toggle="tooltip" onclick="deleteAd('.$row['ad_id'].')"><i class="material-icons"></i> </a>' ;
您可以尝试使用如下键和值的关联数组:
$output['data'][] = array( $x, $row['address'], $row['single_unit'], $row['single_rent'], $row['sharing_unit'], $row['sharing_rent'], $row['date'], 'buttons' => $actionButton // no colon here );
并尝试
row.append('<td>' + obj.buttons + '</td>');
使用可帮助您修复语法错误的代码编辑器。此外,使用浏览器开发工具(F12在 Windows 上和在 Mac 上 ++ cmd)查找 JavaScript 错误。optioni
推荐阅读
- node.js - OAuth2 刷新令牌 - 如何更新?
- node.js - 尝试通过在 reactjs 中使用 GET 将状态设置为一个值,
- php - 我无法使用路由器的 IP 从 MySql 服务器访问数据
- terraform - 不同 s3 存储桶中的 Terraform 工作区状态?
- python - PostgreSQL 域反映为 sqltypes.NULLTYPE
- android - Android PackageManager获取资源号0x00000000的值时没有包标识符
- sql - 从组中返回特定行的 SQL 查询
- jquery - HTML动态元素有不同的大小
- c# - 带有 lambda 表达式的字典
- junit - 如何模拟没有参数的 void 方法?