2012-02-05 47 views
1

我正在嘗試爲我的網站製作表單,以便輕鬆地傳輸一些數據。
我想在表單提交時調用一個小的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> 
+4

請發佈呈現的HTML。 – j08691 2012-02-05 21:45:00

回答

2

如果您不想提交表單,您需要buyItem函數中的return true;。你沒有足夠的業力返回假,但沒有足夠的,這意味着函數返回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(); ?>

+0

getKarma返回一個值,當你沒有足夠的業力時,表單不能被提交, – Jonathan 2012-02-05 22:05:29

+0

我添加了完整的文檔,所以你可以看到其他函數 – Jonathan 2012-02-05 22:15:58

+0

鑑於你發佈了額外的代碼,你需要改變'var currKarma = <?php getKarma(); ?>;'var'currKarma = <?php echo getKarma(); ?>;'還要確保將返回true添加到buyItem的結尾。另外請確保在服務器端檢查這些相同的值,因爲用戶可以很容易地在客戶端欺騙他們的業力,或繞過驗證並提交表單而不考慮業力。 – drew010 2012-02-05 22:25:09

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 

我已經在這裏做了兩兩件事:從PHP代碼

  1. 刪除HTML。
  2. 向傳遞給JavaScript的值添加json_encode。這可能看起來微不足道,但對於更復雜的數據json_encode是一種真正的生活救星。

腳本壞掉的原因很可能是因爲JavaScript中發生了語法錯誤。

<?php getKarma(); ?>是做什麼用的?

+0

它給出了價值回來,(現場貨幣) – Jonathan 2012-02-05 22:02:55

+0

我加了全部文件,;) – Jonathan 2012-02-05 22:15:22