如何在Javascript上传递php数据

Currently I use this code to display the result on the screen

$sqlUrl = "SELECT *
            FROM $category
            WHERE sub_category = '$subCategory'";

$result = mysqli_query($con,$sqlUrl);
echo "<ul>";
while($row = mysqli_fetch_array($result))
  {
  echo "<li>";
  echo "<a href='$row[url]'>$row[link_text]</a>";
  echo "</li>";
  }
echo "</ul>";

but using an other template html file the above code displays the results at the very beginning of the page. So, I have place a div in html template

<div id="div6">

</div>

and I am to use JScript so as for the results to display at the div place. The script looks like

<script>
document.getElementById("div6").innerHTML= "XXXX";
</script>

Could you please let me know what to write on the XXXX place?

I don't have any experience with phpBB, so I can't provide much help with how to use the template, unfortunately. But I recommend that you do more research to figure out how to do it correctly by using the template to display it.

If you really want to go the JavaScript route, you can edit your PHP code to give the ul a unique ID, so echo "<ul>"; would become echo "<ul id='some-id'>";, then your JavaScript code should look something like this:

document.getElementById('div6').appendChild(document.getElementById('some-id'))‌​;

As you have found out, anything echo'd from the php file is displayed before the template is parsed, so you need to assign template variables (or in this case block variables because the output is from a loop).

In your php file:

$sqlUrl = "SELECT *
            FROM $category
            WHERE sub_category = '$subCategory'";

$result = mysqli_query($con,$sqlUrl);

while($row = mysqli_fetch_array($result))
  {
  $template->assign_block_vars('loopname', array(
    'THE_URL'       => $row[url],
    'THE_LINK_TEXT' => $row[link_text]
  ));
  }

then inside your template file:

<li>
<!-- BEGIN loopname -->
  <a href="{loopname.THE_URL}">{loopname.THE_LINK_TEXT}</a>
<!-- END loopname -->
</ul>