duanhuang7591 2016-07-02 08:44
浏览 50
已采纳

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.

  • 写回答

1条回答 默认 最新

  • duadpnld426905 2016-07-02 09:02
    关注

    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'];
    
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染