所以我有一个index.html下面的代码。我想从我的GetVariable.php文件中获取变量“variableFromMyPHPscript”到我的index.html中。从PHP脚本获取变量为HTML脚本?
<!doctype html>
<html>
<head>
<meta charset = "utf-8">
</head>
<body>
<p>Random Text....</p>
</body>
</html>
<script>
console.log(variableFromMyPHPscript);
</script>
这里是我的PHP脚本(GetVariable.php),它使用JSON从我的数据库中的值,以便它可以被存储为一个变量。
<script>
var variableFromMyPHPscript = <?php
$link = mysqli_connect('....', '....', '....', '....');
$query = "select * from thunderDemo";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result))
{
if($row["ThunderOrNot"] == 'Thunder')
{
echo json_encode("Thunder1");
}
}
mysqli_close($link);
?>;
</script>
那么我将如何去获得“variableFromMyPHPscript”的值从我的PHP文件到我的HTML文件,这样我就可以在HTML文件中使用JavaScript使用它呢?感谢您的任何帮助!
使用ajax请求获取php变量在jquery/javascript – Poria
PHP脚本不能放置在脚本标记之间,就像你做的那样。看看这个:http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming –
可能的重复[如何访问PHP变量在JavaScript或jQuery而不是<?php echo $ variable?>](http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php -echo-VARI) –