js,mysql,php - 通过 while 循环在 ajax 请求中获取特定结果
js,mysql,php - Get specific result in the ajax request via while loop
当我在 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'];
}
?>
但是当我点击第二个或第三个按钮时,结果显示在第一个按钮中。
通过自动递增区分 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'];
}
?>
当我在 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'];
}
?>
但是当我点击第二个或第三个按钮时,结果显示在第一个按钮中。
通过自动递增区分 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'];
}
?>