gpt4 book ai didi

javascript - 如何将从循环中获得的 Ajax 变量附加到 标记中

转载 作者:行者123 更新时间:2023-12-04 08:41:36 25 4
gpt4 key购买 nike

大家好,我是 Ajax 的新手,我正在使用 ajax 从我的数据库中以对象数组的形式从单独的 PHP 文件中获取值并将它们显示在表中,一切正常,但我想创建一个链接和附加从数组中获得的 ID,以便我可以向另一个 PHP 文件发送 GET 请求,该文件将对该特定行进行操作。
PHP 文件的代码

<?php
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<data.length;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 +="<tr>";
html +="<td>"+"<a class='link' href=''>Delete</a>"+"</td>";
html +="<td>" + name + "</td>";
html +="<td>" + email + "</td>";
html +="<td>" + subject + "</td>";
html +="<td>" + message + "</td>";
html +="</tr>";


}
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<data.length;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 +="<tr>";
html +=`<td><a href='${link}'>Delete</a></td>`;
html +="<td>" + name + "</td>";
html +="<td>" + email + "</td>";
html +="<td>" + subject + "</td>";
html +="<td>" + message + "</td>";
html +="</tr>";


}
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 +=`<td><a href='${link}'>Delete</a></td>`;`
Expected eof but found error ` }`

最佳答案

关于javascript - 如何将从循环中获得的 Ajax 变量附加到 <a href ='' '/a> 标记中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64544304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com