gpt4 book ai didi

php - 表单提交时的 Javascript 不运行

转载 作者:行者123 更新时间:2023-11-30 10:44:36 25 4
gpt4 key购买 nike

我正在尝试为我的网站制作一个表格,以便轻松传输一些数据。
我试图在表单提交时调用一个小的 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>

最佳答案

您需要 return true;来自 buyItem如果您不想提交表单,请执行此操作。你在没有足够业力的情况下返回 false,但没有足够的业力,这意味着该函数返回 undefined这会阻止表单提交。

尝试:

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;
}

return true;
}

根据函数实际执行的操作,您可能需要更改 <?php getKarma(); ?><?php echo getKarma(); ?> .

关于php - 表单提交时的 Javascript 不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com