簡體   English   中英

在html中使用javascript和php。 群眾混亂

[英]Using javascript and php in html. mass confusion

我正在嘗試制作將在移動設備上工作的php文檔。 我們有一些JavaScript可以在應用程序頂部顯示用於導航的按鈕。 我的php文檔應該顯示在按鈕的正下方。 它應該是一個相當簡單的頁面,但我的工作非常糟糕。 我試圖簡單地調用一個設置變量的函數。 我想使用此變量來顯示使用該應用程序的人所擁有的金額。 我遇到了函數和變量的問題。 有人告訴我html不能使用變量或函數,所以我需要闖入php並使用“ echo functionName();”:聲明函數,調用函數以設置變量,在div內的頁面上顯示變量。任何幫助將不勝感激,這是我的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php

?>
<html>
<head>

    <script type="text/javascript">
    //int $availableBal;
    //int $currentHoldings;
    function int getBalance();
    {

        int $availableBal = 500;
        //alert("you have " + $availableBal + " dollars to spend");
        return $availableBal;
    }

    function void getHoldings()
    {
        int $MSFT_Shares = 5;
        int $MFST_Price= 100;
        int $currentHoldings = $MSFT_Shares * $MSFT_Price;
        return $currentHoldings;
    }
    </script>   
    <style>
html {text-align:center}
</style>
</head>
<body>
    <div id="totalMoney" align="center" style= "border:1px solid grey">         
            You have a current balance of: $ <?php 'echo getBalance();' ?> 
            <br>
            <!--The total worth of your current stocks is: $ <script type="text/javascript"> document.write(getHoldings();)</script> -->
    </div>
</body>
</html>

您已經將兩種語言混合在一起。 Javascript是一個客戶端應用程序,其中PHP是服務器端的。

所有這一切都可以通過兩者實現。我已經模擬了一個可以在javascript中使用的工具,並帶有一些jquery來顯示。 http://jsfiddle.net/Nj3ce/

<!DOCTYPE >
<html>
<head>
    <script text="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
           //on document load this function will be fired
           //automatically runs getHoldings();
           var holdings = getHoldings();
            $('#totalMoney span').text(holdings);
        });

        function getBalance(){
            var availableBal = 500;
            return availableBal;
        }

        function getHoldings(){
            var MFST_Shares = 5;
            var MFST_Price = 100;
            var currentHoldings = MFST_Shares * MFST_Price;
            return currentHoldings;
        }​
    </script>
    <style type="text/css">
        html {text-align:center}​
    </style>
</head>
<body>
    <div id="totalMoney" align="center" style= "border:1px solid grey">         
            You have a current balance of: $<span></span>
            <br>
    </div>
</body>
</html>​

在這種情況下,應嘗試以每種語言分別聲明函數。 您只想用php完成所有要做的事情。 為了干凈起見,請使用<!DOCTYPE>並移到php上方。 另外,在所有代碼中都包含一個單獨的文件,並讓html的CSS / JavaScript / php更少

您無需在javascript中聲明數據類型。 (即function intint $availableBal )。 所有這些都可以用PHP完成。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<?php
    // Should really put this in an external file
    function getBalance()
    {
        $iAvailableBal = 500;
        return $iAvailableBal;
    }

    function getHoldings()
    {
        $iMSFTShares = 5;
        $iMSFTPrice = 100;
        $iCurrentHoldings = $iMSFTShares * $iMSFTPrice;
        return $iCurrentHoldings;
    }
?>
<style type="text/css">
/* Should really put this in an external file */
html {text-align:center}
</style>
</head>
<body>
    <div id="totalMoney" align="center" style= "border:1px solid grey">         
            You have a current balance of: $ <?php echo getBalance(); ?> 
    </div>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM