当我在while循环中单击按钮时,我想通过ajax仅显示某个结果。
while($row = mysql_fetch_array($rs_admin))
{
echo ("<h3>" .$row['emer_type'] . "</h3> ");
echo ("<p>".$row['emer_desc'] . "</p>");
echo '<div class="button"><button type="button" onclick="loadDoc()">Change Content</button></div>':
echo '<div id="demo"><p>I WANT TO PUT IT HERE</p></div>';
}
他们拥有自己的ID,以便他们知道他们将获取什么。 这是我有的ajax
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "test.php", true);
xhttp.send();
}
</script>
test.php就是这个
<?php
include 'includes/db_connection.php';
$sel_admin = "select * from ehr_cm_emergency ";
$rs_admin = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin))
{
echo $row['emer_more'];
}
?>
然而,当我点击第二个或第三个按钮中的按钮时,结果显示在第一个按钮中。
答案 0 :(得分:1)
通过自动增量区分id
并将其传递给函数,并将文本应用于id,如下所示,
<?php
$i=0;
while($row = mysql_fetch_array($rs_admin))
{
$i++;
echo ("<h3>" .$row['emer_type'] . "</h3> ");
echo ("<p>".$row['emer_desc'] . "</p>");
echo '<div class="button"><button type="button" onclick="loadDoc(\'demo'.$i.'\')">Change Content</button></div>';
echo '<div id="demo'.$i.'"><p>I WANT TO PUT IT HERE</p></div>';
}
?>
<script>
function loadDoc(id) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById(id).innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "test.php", true);
xhttp.send();
}
</script>
针对特定的基于行的 我使用emp_id作为唯一,改变你的价值
<?php
$i=0;
while($row = mysql_fetch_array($rs_admin))
{
$i++;
echo ("<h3>" .$row['emer_type'] . "</h3> ");
echo ("<p>".$row['emer_desc'] . "</p>");
echo '<div class="button"><button type="button" onclick="loadDoc(\'demo'.$i.'\','.$row['emp_id'].')">Change Content</button></div>';
echo '<div id="demo'.$i.'"><p>I WANT TO PUT IT HERE</p></div>';
}
?>
<script>
function loadDoc(id,empid) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById(id).innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "test.php?empid="+empid, true);
xhttp.send();
}
</script>
<?php
include 'includes/db_connection.php';
$sel_admin = "select * from ehr_cm_emergency where emp_id=".$_GET['emp_id'];
$rs_admin = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin))
{
echo $row['emer_more'];
}
?>