js,mysql,php - 通过while循环获取ajax请求中的特定结果
问题描述:
I want to display only a certain result via ajax when I click on a button while it is in the while loop.
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>';
}
They have their very own ID so that they know what they will fetch. This is the ajax that I've got
<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>
The test.php is this
<?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'];
}
?>
However when I'm clicking the button in the second or third button, the result is displaying in the first one.
当我在while循环中单击按钮时,我想通过ajax仅显示某个结果。 p>
while($ row = mysql_fetch_array($ rs_admin))
{
echo(“&lt; h3&gt;”。$ row ['emer_type']。“&lt ; / h3&gt;“);
echo(”&lt; p&gt;“。$ row ['emer_desc']。”&lt; / p&gt;“);
echo'&lt; div class =”button“&gt;&lt; button type =“button”onclick =“loadDoc()”&gt;更改内容&lt; / button&gt;&lt; / div&gt;':
echo'&lt; div id =“demo”&gt;&lt; p&gt;我想把它放在这里&lt; p ; / p&gt;&lt; / div&gt;';
}
code> pre>
他们拥有自己的ID,以便他们知道要取的内容。
这是 我有的ajax p>
&lt; script&gt;
function loadDoc(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4&amp;&amp; xhttp.status == 200){
document.getElementById(“demo”)。innerHTML = xhttp.responseText;
}
};
xhttp。 open(“GET”,“test.php”,true);
xhttp.s end();
}
&lt; / script&gt;
code> pre>
test.php是这个 p>
&lt;?php
include'include / 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'];
}
?&gt;
code> pre>
但是当我点击第二个或第三个按钮中的按钮时,结果显示在第一个或第三个按钮中 。 p>
div>
答
Differentiate the id
by auto increment and pass it to the function, and apply to the text to the id like following,
<?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>
For specific Row based I used emp_id for unique, change your value
<?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'];
}
?>