首页 > 解决方案 > getElementById(php 变量)不起作用

问题描述

我有一个由于 mysql 查询而创建的列表,还有几个<li>具有唯一 ID 作为变量的列表。

当我想把这个ID放进去时getElementById(<?php $id?>)它不起作用。我已经尝试了所有的报价,但没有奏效。

这是代码;

<ul >
<?php
  $query= mysqli_query($con,"Select * from database order by date ASC");
  while($row = mysqli_fetch_array($query)){?>
  <li onclick="showMlstone(this.value)" value="<?php echo $row[id];?>"><?php echo $row[name];?><p id=<?php echo $row[id];?>></p></li>
  <script>
  function showMlstone(int) {
    var xhttp;
      if (int == "") {
      document.getElementById(<?php echo $row[id];?>).innerHTML = "";
      return;
    }
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
      document.getElementById(<?php echo $row[id]?>).innerHTML = this.responseText;
      }
    };
    xhttp.open("GET", "get.php?q="+int, true);
    xhttp.send();
  }
  </script>
<?php
</ul>

感谢您的帮助

标签: javascriptphphtmlmysql

解决方案


尝试使用引号:

document.getElementById("<?php echo $row['id']?>").innerHTML = this.responseText;

推荐阅读