Javascript at form submit does not run
<p>i'm trying to make a form for my website, to transfer some data easily.<br>
I am trying to call in a small javascript when the form submits, but it doesn't run.</p>

<p>below this you can see the PhP function in wich i make my form (in a small table)
and below that i'll enter my javascript.</p>

<p>Any help on this matter would be useful</p>

<p>thanks in advance</p>

<p>php:</p>

<pre><code>function addrow($itemText , $itemPrice , $itemID , $odd)
{
     if ($odd)
     {
            echo "&lt;tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7&gt;";
     }
     else
     {
            echo "&lt;tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF&gt;";
     }
     echo "&lt;td style=\"width: 70%;\"&gt;$itemText&lt;/td&gt;";
     echo "&lt;td style=\"width: 10%; padding-left: 5px;\"&gt;&lt;b&gt;$itemPrice&lt;/b&gt;&lt;/td&gt;";
     if (hasRole($itemID))
     {
          echo "&lt;td style=\"width: 15%; padding-left: 5px;\"&gt;&lt;b&gt;Unlocked&lt;/b&gt;&lt;/td&gt;";
     }
     else
     {
          echo "&lt;td style=\"width: 15%; padding-left: 5px;\"&gt;&lt;b&gt;&lt;form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" &gt;";
          echo "&lt;input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\"&gt;";
          echo "&lt;input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" &gt;";
          echo "&lt;input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" &gt;";
          echo "&lt;input name=\"Send\" type=\"submit\" value=\"       Buy Now       \" /&gt;&lt;/form&gt;&lt;/b&gt;&lt;/td&gt;";
     }     
     echo "&lt;/tr&gt;";
}
</code></pre>

<p>javascript:</p>

<pre><code>function buyItem(reqKarma)
{
       var currKarma = &lt;?php getKarma(); ?&gt;;
       alert(currKarma +"");
       if (currKarma &lt; reqKarma)
       {
            alert('You do not have enough Karma to buy this title.');
            return false;
       }
}
</code></pre>

<p>Full document:</p>

<pre><code>&lt;?php
include "php-scripts/DBConnection.php";
$con = getconnection();
mysql_select_db("brokendi_BD", $con);
loadpage();

function loadpage()
{
      echo "&lt;table cellpadding=\"0\" cellspacing=\"0\" style=\"width: 98%\" &gt;";
      echo "&lt;tr class=\"info-row\" bgcolor=#252525 style=\"color:white;  height: 15px;\"&gt;";
      echo "&lt;td style=\"width: 70%; height: 10px; padding-left: 5px;\"&gt;&lt;b&gt;Item Name&lt;/b&gt;&lt;/td&gt;";
      echo "&lt;td style=\"width: 10%; height: 10px; padding-left: 5px;\"&gt;&lt;b&gt;Item Price&lt;/b&gt;&lt;/td&gt;";
      echo "&lt;td style=\"width: 15%; height: 10px; padding-left: 5px;\"&gt;&lt;b&gt; &lt;/b&gt;&lt;/td&gt;";
      echo "&lt;/tr&gt;";
      addrow("test",1,1,false);
      echo "&lt;/table&gt;";
}

function addrow($itemText , $itemPrice , $itemID , $odd)
{
     if ($odd)
     {
            echo "&lt;tr class=\"content-row online\" id=\"958\" bgcolor=#A7A7A7&gt;";
     }
     else
     {
            echo "&lt;tr class=\"content-row online\" id=\"958\" bgcolor=#BFBFBF&gt;";
     }
     echo "&lt;td style=\"width: 70%;\"&gt;$itemText&lt;/td&gt;";
     echo "&lt;td style=\"width: 10%; padding-left: 5px;\"&gt;&lt;b&gt;$itemPrice&lt;/b&gt;&lt;/td&gt;";
     if (hasRole($itemID))
     {
          echo "&lt;td style=\"width: 15%; padding-left: 5px;\"&gt;&lt;b&gt;Unlocked&lt;/b&gt;&lt;/td&gt;";
     }
     else
     {
          echo "&lt;td style=\"width: 15%; padding-left: 5px;\"&gt;&lt;b&gt;&lt;form name=\"buy\" action=\"php-scripts/BuyItem.php\" onsubmit=\"return buyItem($itemPrice)\" &gt;";
          echo "&lt;input type=\"hidden\" name=\"UserID\" value =\"".getUID()."\"&gt;";
          echo "&lt;input type=\"hidden\" name=\"itemID\" value = \"".$itemID."\" &gt;";
          echo "&lt;input type=\"hidden\" name=\"reqKarma\" value = \"".$itemPrice."\" &gt;";
          echo "&lt;input name=\"Send\" type=\"submit\" value=\"       Buy Now       \" /&gt;&lt;/form&gt;&lt;/b&gt;&lt;/td&gt;";
     }     
     echo "&lt;/tr&gt;";
}


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-&gt;uid) 
     { 
          $userID=$user-&gt;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() &gt;= $requiredKarma)
        {
                 return true;
        }
        else
        {
                return false;
        }      
}
?&gt;

&lt;script type="text/javascript"&gt;

function buyItem(reqKarma)
{
       var currKarma = &lt;?php getKarma(); ?&gt;;
       alert(currKarma +"");
       if (currKarma &lt; reqKarma)
       {
            alert('You do not have enough Karma to buy this title.');
            return false;
       }
}

&lt;/script&gt;
</code></pre>
