表单提交时的Javascript不会运行

时间:2012-02-05 21:43:54

标签: php 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>

2 个答案:

答案 0 :(得分:2)

如果您要提交表单,则需要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(); ?>

答案 1 :(得分:2)

用PHP编写完整的HTML真的不是一个好主意,它只是凌乱。

变化:

echo "<td style=\"width: 15%; padding-left: 5px;\"><b><form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" >";

为:

?>
<script type="text/javascript">
function buyItem<?php echo $itemID;?> {
    buyItem(<?php echo json_encode($itemPrice);?>);
}
</script>
<td style="width: 15%; padding-left: 5px;"><b><form name="buy" action="php-scripts/BuyItem.php" onsubmit="return buyItem<?php echo $itemID;?>()">
<?php

我在这里做了两件事:

  1. 从PHP代码中删除了HTML。
  2. 为传递给JavaScript的值添加了json_encode。这似乎微不足道,但对于更复杂的数据json_encode是一个真正的救星。
  3. 由于JavaScript中出现语法错误,可能导致脚本损坏。

    <?php getKarma(); ?>做了什么?