gpt4 book ai didi

javascript - 如何使用 php 从 sql 查询中获取完整结果,然后使用 javascript 显示该结果

转载 作者:行者123 更新时间:2023-11-30 15:43:56 25 4
gpt4 key购买 nike

我有一个执行查询的 PHP 脚本。然而,结果可以是多行。如何对数据行进行 json_encode,然后使用 javascript 获取该数据并将其显示在表格中?

这个文件回显了两行json编码,每行代表一行

<?php  
include("config.php");
session_start();
$user = $_SESSION["loggedIn"];
$sql = "SELECT * FROM tickets where SenderName =\"$user\" OR SenderName = \"Yoon\"";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo json_encode($row);
}
?>

我想使用 javascript 脚本和 ajax 获取两行,然后创建一个函数,根据结果创建一个表

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="buttons.css">
<script type="text/javascript">
function processData() {
var httpRequest;
//alert("hi there");

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.open('GET', 'userTables.php', true);

httpRequest.onreadystatechange = function() {
showTickets(httpRequest);
}
httpRequest.send();
}
function show(){
document.getElementById("buttons").style.visibility= "hidden" ;
}
function showTickets(httpRequest){
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200){
alert("hi there");
var data = JSON.parse(httpRequest.responseText);
alert(data["SenderName"]); //I can only get the data in a key value array if i have just one row as the result
}
else{
alert('Problem with request');
}
}
}

function createTable(httpRequest){
//need to add innerhtml to some div that shows the table
}



</script>
</head>
<body>
<h1>Welcome! </h1>
<div id="buttons">
<button class="myButton" onclick="processData()">View My Tickets</button>
<button class="myButton">Submit Ticket</button>
<button class="myButton">Change my Password</button>
</div>
<div id = "table" >

</div>
</body>
</html>

我认为 html 看起来会像这样显示表格:

<table align="left" cellspacing="5" cellpadding="8"> 
<tr><td align="left"><b>TicketNum</b></td>
<td align="left"><b>Recieved</b></td>
<td align="left"><b>SenderName</b></td>
<td align="left"><b>Sender Email</b></td>
<td align="left"><b>Subject</b></td>
<td align="left"><b>Tech</b></td>
<td align="left"><b>Status</b></td>
<td align="left"><b>Select</b></td></tr>';

// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = $result->fetch_assoc() ){

echo '<tr><td align="left">' .
row['TicketNum'] . '</td><td align="left">' .
row['Recieved'] . '</td><td align="left">' .
row['SenderName'] . '</td><td align="left">' .
row['SenderEmail'] . '</td><td align="left">' .
row['Subject'] . '</td><td align="left">' .
row['Tech'] . '</td><td align="left">' .
row['Status'] . '</td><td align="left">' .

'</td><td align="left">';
</table>';

最佳答案

1 确保在 php 中只输出所有行一次

$result = $conn->query($sql);
$data = array();
while($row = $result->fetch_assoc()){
$data[] = $row;
}
exit(json_encode($data)); // there probably is nothing else, just exit

2 在您的 javascript 中尝试使用控制台而不是警报 - 如果您需要这方面的帮助,这可能是一个新问题,但是在 firefox/chrome 中谷歌“控制台”调试。

var data = JSON.parse(httpRequest.responseText);
console.log(data); // you will have an object of the rows now

3 现在您需要使用 javascript 通过创建 html 来填充所有行...试试这样的操作

var data = JSON.parse(httpRequest.responseText);
for (var key in data) {
// skip loop if the property is from prototype
if (!data.hasOwnProperty(key)) continue;

var row = data[key];
console.log(row);
// now you need to make your html using javascript
// how to do that is a new question, imho
}

关于javascript - 如何使用 php 从 sql 查询中获取完整结果,然后使用 javascript 显示该结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391870/

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