表单提交时的Javascript未运行


Javascript at form submit does not run

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

如果您不想提交表单,则需要从buyItem函数中return true;。在没有足够因果报应的情况下返回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编写完整的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是一个真正的救命稻草

脚本损坏的原因可能是JavaScript中出现语法错误。

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