JavaScript does not work inside PHP loop
<p>It seems like when i echo out the result in php, it will loop through the database and display it in the "echo" below but if I try to display it using javascript, it does not even loop. what's the problem in the javascript?</p>

<pre><code>&lt;?php
while($row = $result-&gt;fetch()){
    $id = $row['id'];

    echo $id; //loops through and displays the all the id's in order in the database
    ?&gt;

    //this only displays the first id and doesn't loop even though it is in the php while loop
    &lt;script&gt;
        document.getElementById("test").innerHTML = "&lt;?= $id ?&gt;";
    &lt;/script&gt;
    &lt;span id="test"&gt;&lt;/span&gt;

    &lt;?php
}
?&gt;
</code></pre>