似乎当我在 php 中回显结果时,它会循环遍历数据库并将其显示在下面的“回显”中,但如果我尝试使用 javascript 显示它,它甚至不会循环。 javascript 有什么问题?
<?php
while($row = $result->fetch()){
$id = $row['id'];
echo $id; //loops through and displays the all the id's in order in the database
?>
//this only displays the first id and doesn't loop even though it is in the php while loop
<script>
document.getElementById("test").innerHTML = "<?= $id ?>";
</script>
<span id="test"></span>
<?php
}
?>
最佳答案
请试试这个
<?php
while($row = $result->fetch()){
$id = $row['id'];
echo $id; //loops through and displays the all the id's in order in the database
?>
//this only displays the first id and doesn't loop even though it is in the php while loop
<!-- change in id name -->
<span id="test_<?=$id?>"></span>
<script type="text/javascript">
//change here
document.getElementById("test_<?=$id?>").innerHTML = "<?= $id ?>";
</script>
<?php
}
?>
关于JavaScript 在 PHP 循环中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137372/