如何根据HTML项目ID动态进行查询



所以,我有这段HTML代码,它只是一个导航栏。

<div class="views">
<b>Views</b>
<hr style="background-color:#f0efef;">
<table id="views_table">
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_pending_cases">My Pending & On Hold Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_solved_cases">My Solved Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_csat_cases">My CSat Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><a href="escalationreview.php"><div>Escalation Review</div></a></td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><a href="wwt1.php"><div>WW Support Tier 1</div></a></td>
</tr>
</table>
</div>  

然后我有了这个php文件,它在我的MySQL数据库中进行查询,并在一个表中显示所有信息。

<?php
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;      
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM escalationreview";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'> 
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else { 
$output= "0 results"; 
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>

现在,我想实现的是:每次我点击导航栏中的一个项目时,我都想调用这个php函数,然后将点击的项目的ID发送到php函数,并根据该ID进行查询,类似于以下内容:

HTML

<tr class="item">
<td class="view" id="WW Support Tier 1" onclick="fill(this.id)"><a href="escalationreview.php"><div>Escalation Review</div></a></td>
</tr>

PHP

<?php
function fill($id){
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;      
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'> 
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else { 
$output= "0 results"; 
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>

我知道上面的代码不起作用,这只是我想象的一个想法。

我相信使用AJAX可以实现这一点,但我不完全确定。如有任何指导,我们将不胜感激!事先谢谢。

如果你的PHP文件呈现了整个html,你可能根本不需要AJAX,只需要用id作为查询参数来填充每个导航项的链接:

<td class="view" id="WW Support Tier 1">
<a href="escalationreview.php?id="<?= urlencode("WW Support Tier 1") ?>"><div>Escalation Review</div></a></td>
</tr>

然后在您的PHP文件中,只需获取ID作为参数

<?php
$id = $_REQUEST["id"] ?? null;
if($id) {
fill($id);
}

function fill($id) {
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;      
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'> 
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else { 
$output= "0 results"; 
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}

echo $output;
}
?>
let formData = new FormData();
formData.append('id', this.id);
$.ajax({
url: "./yourPage.php",
type: "POST",
data:formData,
processData: false,
contentType: false,
complete: (response) => {
console.log(response.responseText)
}
})

在您的Page.php中:

function fill($_REQUEST["id"]){ .... your code that you wrote above}

最新更新