2017-04-07 51 views
-1

我正在使用AdminLTE免費模板創建一個管理網站我有一個問題,從PHP中獲取一個變量從括號中打印到HTML頁面 我想要做類似這樣的:php中的變量html

<!DOCTYPE html> 
 
<html> 
 
    <head > 
 
<!-- rest of the page --> 
 
    <span class="info-box-text" name= "msg" > 
 
\t \t \t <!-- I want to print x here inside the info box --> 
 
\t \t \t \t <script> <?php echo $x ?> </script> 
 

 
\t </span> 
 
    <!-- the function that has the output comes after it is called --> 
 
    <script> 
 
    <?php 
 
     do somthing to $x 
 
     $output_messege = "the result of something=".$x"."  
 
    ?> 
 
    </script> 
 
    
 
    
 
    </head > 
 
</html>

+0

你得到的結果是什麼? PHP是否安裝? – mkaatman

+0

您是否收到任何錯誤? –

回答

1

的原因,你的代碼不能正常工作是因爲你裹在腳本標籤回聲。請記住,PHP在服務器上執行,它發出一個純HTML。因此,當您的代碼進入瀏覽器時,回顯的變量將顯示在腳本標記中,而不會顯示在頁面上!

所以,你可以使用「回聲」命令簡單的文本或「printf」式打印輸出格式化字符串(有用的打印陣列等)

<!DOCTYPE html> 
<html> 
    <head> 
    <?php 
      // let's do the 'somthing to $x' so that later refrence has the updated $x value 
      $output_messege = "the result of something=".$x"."  
     ?> 
    </span> 
</head> 
<body> 
<span class="info-box-text" name="msg"> 
    <!-- we can print the output_message here with a simple echo. notice that there no script tags ! --> 
    <?php echo $output_messege ?> </span> 
</body> 
</html> 

編輯: 要細說,我覺得你誤解了PHP和JS的工作方式。 PHP在服務器上工作。當服務器通過「PHP渲染器」呈現頁面時,它會找到<?php ?>標記之間的所有內容並執行它們。所以,如果你在那裏放置一個回聲,它會按照預期的那樣做,在那個點回顯變量或字符串。 畢竟這是一個簡單有效的HTML頁面,沒有PHP邏輯或代碼生成,這就是瀏覽器接收到的內容。現在JS開始發揮作用。 瀏覽器現在可以找到<script> </script>標籤下的所有內容並執行它。

+0

非常感謝你... –