ajax append a href,javascript - How to append a Ajax variable obtained from a loop into a <a href=''...

Hi everyone i'm new to Ajax,i'm using ajax to get values from my database in form of an object array from a separate PHP file and displaying them in a table,everything works fine,but i want to create a link and append the ID obtained from the array so that i can send a GET request to another PHP file which will act upon that particular row.

code for the PHP File

include 'Module/Credentials.php';

$sql="SELECT * FROM queries";

$query= mysqli_query($connection, $sql) or die(mysqli_error($connection));

$data = array();

while ($row = mysqli_fetch_object($query))

{

array_push($data, $row);

}

echo json_encode($data);

`

Ajax code

javascript

` var ajax=new XMLHttpRequest();

var method="GET";

var url="getMessages.php";

var asynchronous=true;

ajax.open(method, url,asynchronous);

ajax.send();

ajax.onreadystatechange=function(){

if(this.readyState==4 && this.status==200){

var data=JSON.parse(this.responseText);

console.log(data);

var html="";

for(var b=0;b

var ID=data[b].ID;

var name=data[b].name;

var email=data[b].email;

var subject=data[b].subject;

var message=data[b].message;

var link = "management.php?delete=";

$(document).ready(function(){

$('.link').attr('href', link+ID);

});

console.log(data.length);

html +="

";

html +="

"+" Delete"+"";

html +="

" + name + "";

html +="

" + email + "";

html +="

" + subject + "";

html +="

" + message + "";

html +="

";

}

document.getElementById("messages").innerHTML += html;

}

}`

the ID on the link is not changing its just displaying a single ID=6

Solution

` var ajax=new XMLHttpRequest();

var method="GET";

var url="getMessages.php";

var asynchronous=true;

ajax.open(method, url,asynchronous);

ajax.send();

ajax.onreadystatechange=function(){

if(this.readyState==4 && this.status==200){

var data=JSON.parse(this.responseText);

console.log(data);

var html="";

for(var b=0;b

var ID=data[b].ID;

var name=data[b].name;

var email=data[b].email;

var subject=data[b].subject;

var message=data[b].message;

var link = `management.php?delete=${ID}`;

html +="

";

html +=`

Delete`;

html +="

" + name + "";

html +="

" + email + "";

html +="

" + subject + "";

html +="

" + message + "";

html +="

";

}

document.getElementById("messages").innerHTML += html;

}

}`

but i'm getting error messages in NetBeans IDE 8.0.2 like

Expected an operand but found error

`var link== `management.php?delete=${ID}`;`

Expected an operand but found error

`html +=`

Delete`;`

Expected eof but found error ` }`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值