简体   繁体   中英

Using javascript and php in html. mass confusion

I'm trying to make a php document that will work on a mobile device. We have some javascript to display buttons at the top of the app for navigation. My php document is suppose to show up right below the buttons. It should be a fairly simple page but I am struggling pretty bad. I am trying to simply call a function that sets a variable. I want to use this variable to display the amount of money the person using the app has. I'm running into problems with the functions and variables. I've been told html can't use varaibles or functions, so I need to break into php and use "echo functionName(); so: declare function, call function to set variable, display variable on the page inside of a div. Any help would be greatly appreciated. Here is my code.:

<!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>

You've mixed the two languages together. Javascript is a client side application, where PHP is server sided.

All this can be achieved with both, I've mocked up one working for you in javascript, with a bit of jquery for display. 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>​

You should try to declare functions separate in each language in this case. All you want to do there can be done solely with php. And for cleanness, take the <!DOCTYPE> and move above the php. also, include a separate file with all the code and let the html with less css/javascript/php

You don't declare datatypes in javascript. (ie function int , int $availableBal ). This can all be done in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM