我正在尝试从数据库中读取信息,并将其放在表中,并且还在每行的最后一列中包含按钮。除了添加按钮之外,我还无法使其正常工作。
这是我的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 = '' +
'' +
''
; // 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 = $('
');row.append('
' + obj.address + '');row.append('
' + obj.single_unit + '');row.append('
' + obj.single_rent + '');row.append('
' + obj.sharing_unit + '');row.append('
' + obj.sharing_rent + '');row.append('
' + obj.date + '');row.append('
' + *Code to include buttions here* + '');$('table').append(row)
});
}
});
});
我用下面的代码做了一些测试,它很好地附加了:
$(document).ready(function(){
var ActionButions ='' +
'' +
'';
var row = $('
');row.append('
' + "Vanderbijlpark, Bowker str, CE4, 12" + '');row.append('
' + "3" + '');row.append('
' + "1500" + '');row.append('
' + "1" + '');row.append('
' + "2500" + '');row.append('
' + "2019/08/14" + '');row.append('
' + ActionButions + '');$('table').append(row);
});
我正在尝试通过从数据库中读取值来复制上面的代码。
最佳答案
该字符串应与串联。 (点),而不是PHP脚本中的+(加号)。
这里的[ad_id]是什么?如果它是数据库表的id列,请使用.$row['ad_id'].。$actionButton = '
οnclick="viewAd('.$row['ad_id'].')">
'.
οnclick="editAd('.$row['ad_id'].')">
'.
οnclick="deleteAd('.$row['ad_id'].')">
';
您可以尝试使用键和值的关联数组,如下所示:
$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('
' + obj.buttons + '');使用可帮助您修复语法错误的代码编辑器。另外,使用浏览器开发人员工具(在Windows上为F12,在Mac上为cmd + option + i)来查找JavaScript错误。