我已经在 Drupal 中建立了一个网站,并且我正在尝试通过构建用户点系统来创建一种有趣的方式让会员参与到该网站中,该系统已经到位,但我正在尝试建立一个他们可以购买的商店'头衔'。
这是我为商店编写的脚本,有一些错误处理,但我遇到了一个问题,
在我的 JavaScript 中,我有一个buyitem( )
带有 2 个变量的函数,我想在我的 PHP 函数中使用它来检查我的数据库中的所有内容,有没有一种方法可以将这些变量从 JavaScript 获取到我编写的 PHP 函数,而无需使用外部 PHP文件?
<?php
include "php-scripts/DBConnection.php";
$con = getconnection();
mysql_select_db("brokendi_BD", $con);
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()
{
if ( getKarma() >= $requiredKarma)
{
return true;
}
else
{
return false;
}
}
function buyRole()
{
$currentKarma = getKarma();
$newkarma = $currentKarma - $requiredKarma;
$userID = getUID();
mysql_query("UPDATE userpoints SET points = '$newkarma' WHERE uid='$userID'");
mysql_query("INSERT INTO users_roles (uid, rid) VALUES ($userID, $roleID)");
}
?>
<script type="text/javascript">
buyItem(1 , 0);
function SetStore()
{
}
Function buyItem(itemID,reqKarma)
{
if (<?php enoughKarma(); ?>)
{
<?php buyRole(); ?>
}
else
{
alert('You do not have enough Karma to buy this title.');
}
}
</script>