这是一个很好的问题!我很乐意为您提供帮助。
首先- 调用您的信息的 XML 文件 (database.xml)(根据需要扩展):
<?xml version = '1.0' encoding='utf-8' ?>
<database>
<row id = '1'>
<user>
<name>Bob</name>
<joined>10-10-2019</joined>
</user>
</row>
<row id = '2'>
<user>
<name>Fred</name>
<joined>10-11-2019</joined>
</user>
</row>
</database>
第二- 一个带有表格的php文件(index.php):
<html>
<head>
</head>
<body>
<table>
<?
$users = simplexml_load_file('database.xml');
foreach ($users-row) as $row {
echo '<tr class = "row '.$row['id'].'">'
echo '<td class = "expand"><div role = "button" class = "expand-button '.$row['id'].'">Expand</div></td>'
echo '<td class = "username">'.$row->name.'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
第三- 带有 XMLHTTPRequest 的 JavaScript 文件调用 php 文件 (app.js):
const getClass = (c) => document.getElementsByClassName(c);
for (let i = 0; i < getClass('expand-button').length; i++) {
getClass('expand-button')[i].addEventListener('click', expand, false);
}
function expand(e) {
let xhr = new XMLHTTPRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
getClass('row '+e.target.className.split(' ')[1]).innerHTML += xhr.responseText;
}
};
xhr.open('GET', 'retrieveUsers.php?rowId='+e.target.parentElement.className.split(' ')[1], true);
xhr.send();
}
第四个-(最后!)处理请求的 php 文件:
<?
$rowId = $_GET['rowId'];
$users = simplexml_load_file('database.xml');
foreach ($users-row) as $row {
if ($row['id'] === $rowId) {
echo '<tr class = "expanded">';
echo 'Username: '.$row->name;
echo '<br />Joined: '.$row->joined;
echo '</tr>';
}
}
?>
告诉我这是否有效