php ajax添加表格,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 = '' +

'' +

''

; // 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错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值