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>


感谢您的帮助


慕的地8271018
浏览 230回答 3
3回答

慕森卡

试试这个。它可能工作document.getElementById('<?php&nbsp;echo&nbsp;$row[id];&nbsp;?>').innerHTML&nbsp;=&nbsp;this.responseText;

尚方宝剑之说

如果您有 *.php 文件,其中包含 php 代码和其他不同的语言 - 使用 open 和 close php 标签。<?php// php code here?><!-- Some html here -->您也可以使用简短的语法 - 结合 html 和 php 代码的最佳方式:<ul>&nbsp; &nbsp; <li><?= $variable ?></li></ul>尝试<?=改用<?php echo- 这是相同的,但首先更好,更易读。在你的情况下,你可以将你的 php 代码移动到文件的顶部,然后使用 php 变量在下面的 html 代码中插入,如下所示:<?php$query = mysqli_query($con,"Select * from database order by date ASC");$rows = mysqli_fetch_array($query);?><ul>&nbsp; &nbsp; <? foreach($rows as $row): ?>&nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?=$row['id']?>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; <? endforeach ?></ul>不要忘了写$row['id'],而不是$row[id]和做的一切,可优化你的代码,使之成为你和其他人在计算器更具可读性:)

摇曳的蔷薇

尝试使用引号:document.getElementById("<?php&nbsp;echo&nbsp;$row['id']?>").innerHTML&nbsp;=&nbsp;this.responseText;
打开App,查看更多内容
随时随地看视频慕课网APP