通过while循环在ajax请求中获得特定的结果
js,mysql,Get specific result in the ajax request via while loop
我想通过ajax只显示一个特定的结果,当我点击一个按钮,而它是在while循环。
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'];
}
?>
相关文章:
- JavaScript循环无法正确计算/显示结果
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- JavaScript-将for循环的多个结果推送到数组中
- 虽然循环只返回最终结果
- 使用函数for循环遍历对象以更改值,然后返回结果
- 为什么这个 fs.readFile 循环没有将其结果推送到我的数组
- 返回循环中调用的回调结果的串联
- 来自 JavaScript 循环的意外结果
- jQuery 循环 JSON 结果,对象作为值
- 嵌套在WHILE循环中的IF语句的结果混淆了我(Javascript)
- javascript循环遍历列表项,并在数组中返回结果
- for循环中的多个随机结果
- 我如何做一个纯Javascript foreach循环并打印出结果firebase
- 如何循环使用JSON(使用AngularJS)并将结果放入Javascript数组中
- While循环未将正确的结果推送到数组
- 而循环和回调返回不同的结果
- Javascript do/while循环显示意外结果
- 为什么我的循环会产生重复的结果
- 我如何知道最后一个异步函数何时在ob" for键中返回结果?循环
- 如何在jade中渲染json对象并通过结果循环