我正在尝试制作一个可以在移动设备上运行的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 int
,int $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>