所以我有一個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) –