我正在嘗試爲我的網站製作表單,以便輕鬆地傳輸一些數據。
我想在表單提交時調用一個小的JavaScript,但它不運行。表單提交的Javascript不會運行
在這個下面,你可以看到PhP函數在我使我的表格(在一個小表) 及以下,我將輸入我的JavaScript。
在這個問題上的任何幫助將是非常有用
在此先感謝
PHP:
function addrow($itemText , $itemPrice , $itemID , $odd)
{
if ($odd)
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
}
else
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
}
echo "<td style=\"width: 70%;\">$itemText</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><b>$itemPrice</b></td>";
if (hasRole($itemID))
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b>Unlocked</b></td>";
}
else
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";
echo "<input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\">";
echo "<input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" >";
echo "<input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" >";
echo "<input name=\"Send\" type=\"submit\" value=\" Buy Now \" /></form></b></td>";
}
echo "</tr>";
}
的javascript:
function buyItem(reqKarma)
{
var currKarma = <?php getKarma(); ?>;
alert(currKarma +"");
if (currKarma < reqKarma)
{
alert('You do not have enough Karma to buy this title.');
return false;
}
}
全文檔:
<?php
include "php-scripts/DBConnection.php";
$con = getconnection();
mysql_select_db("brokendi_BD", $con);
loadpage();
function loadpage()
{
echo "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 98%\" >";
echo "<tr class=\"info-row\" bgcolor=#252525 style=\"color:white; height: 15px;\">";
echo "<td style=\"width: 70%; height: 10px; padding-left: 5px;\"><b>Item Name</b></td>";
echo "<td style=\"width: 10%; height: 10px; padding-left: 5px;\"><b>Item Price</b></td>";
echo "<td style=\"width: 15%; height: 10px; padding-left: 5px;\"><b> </b></td>";
echo "</tr>";
addrow("test",1,1,false);
echo "</table>";
}
function addrow($itemText , $itemPrice , $itemID , $odd)
{
if ($odd)
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7>";
}
else
{
echo "<tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF>";
}
echo "<td style=\"width: 70%;\">$itemText</td>";
echo "<td style=\"width: 10%; padding-left: 5px;\"><b>$itemPrice</b></td>";
if (hasRole($itemID))
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b>Unlocked</b></td>";
}
else
{
echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";
echo "<input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\">";
echo "<input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" >";
echo "<input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" >";
echo "<input name=\"Send\" type=\"submit\" value=\" Buy Now \" /></form></b></td>";
}
echo "</tr>";
}
function getKarma()
{
$result = mysql_query("SELECT * FROM userpoints WHERE uid='getUID()'");
$row = mysql_fetch_array($result);
$currentkarma = (int)$row['points'];
return $currentkarma;
}
function getUID()
{
global $user;
if ($user->uid)
{
$userID=$user->uid;
return $userID;
}
else
{
header('Location: http://brokendiamond.org/?q=node/40');
}
}
function hasRole($roleID)
{
$usersid = getUID();
$returnValue = false;
$result = mysql_query("SELECT * FROM users_roles");
while ($row = mysql_fetch_array($result))
{
if ($row['uid'] == $usersid)
{
if ($row['rid'] == $roleID)
{
$returnValue = true;
break;
}
}
}
return $returnValue;
}
function enoughKarma($requiredKarma)
{
if (getKarma() >= $requiredKarma)
{
return true;
}
else
{
return false;
}
}
?>
<script type="text/javascript">
function buyItem(reqKarma)
{
var currKarma = <?php getKarma(); ?>;
alert(currKarma +"");
if (currKarma < reqKarma)
{
alert('You do not have enough Karma to buy this title.');
return false;
}
}
</script>
請發佈呈現的HTML。 – j08691 2012-02-05 21:45:00