<html>
<head>
<script type="text/javascript">
var t;
alert(t);
</script>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$t_id = 6;
$con = new PDO("mysql:host=$dbhost;dbname=testing",$dbuser,$dbpass);
$q = $con-> prepare("query");
$q -> bindParam(1,$t_id);
$q -> execute();
$res = $q->fetchAll();
foreach($res as $r)
{
$ab = $r[0];
$abc = $r[1];
}
echo $ab;
echo $abc;
?>
<script type="text/javascript">
t = <?php echo $abc;?>;
</script>
</body>
</html>
当我在分配 php 变量后提醒“t”变量时,它工作正常。但我想在页面的头部使用“t”。实际上我只想从 DB 中设置 JS 变量。怎么做?