I have a sales transaction form that will record the product and quantity purchased. My problem is when i clicked the submit button, the check function will check if the value in quantity is empty.if empty then the alert box will be displayed and the form will not be submitted.
But when i clicked the add button which is a submit button, the alert box will be displayed and the form will be submitted no matter if i enter the quantity or i didn't enter it. So the problem is with my check function. Please see what's wrong with it.Thx.
<?php
session_start();
include "common_db.inc";
$link_id = db_connect();
if(!$link_id) die (sql_error());
mysql_select_db("project", $link_id) or die (sql_error());
?>
<html>
<head>
<title>Transaction form</title>
<SCRIPT SRC="menu.
js"></SCRIPT>
<SCRIPT SRC="mymenus.
js"></SCRIPT>
<script>
function isDigit (c)
{
return ((c >= "0") && (c <= "9"))
}
function validate(obj)
{
value = obj.value;
for (n = 0; n < value.length; n++)
if (!isDigit(value.charAt(n)))
{
obj.select();
obj.focus();
alert("Field must be numeric");
return false;
}
}
function check(text)
{
if((!text.value)|| (text.value.length==0))
{
alert("Please enter quantity");
text.select();
text.focus();
return false;
}
}
</script>
</head>
<body background="blue2.jpg">
<form name="salestransactionform" METHOD="POST" ACTION="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
if($_POST['btnSearchCust']=='Search')
{
sales( );
$query6 = "insert into sales values('$_POST[salesID]', '$_POST[date]', '$_POST[custID]', '$_POST[empID]') ";
$result6 = mysql_query ($query6);
if(!$result6) error_message (sql_error());
}
if($_POST['addprod']=='Add Product')
{
if($_POST[quantity])
{ sales( );
$now = gmdate("Y-m-d");
$query1 = "select diskaun_price from price_master
where prod_id=\"$_POST[prodID]\" and start_date>=$now and end_date>=$now ";
$result1 = mysql_query ($query1);
if(!$result1) error_message (sql_error());
$NumRows = mysql_num_rows($result1);
if($NumRows)
{
while($row1 = mysql_fetch_array($result1))
$price = $row1["diskaun_price"];
}
else
{
$query2 = "select prod_price from product where prod_id=\"$_POST[prodID]\" ";
$result2= mysql_query ($query2);
if(!$result2) error_message (sql_error());
while($row2 = mysql_fetch_array($result2))
$price = $row2["prod_price"];
}
$query1 = "select * from sales_detail where prod_id=\"$_POST[prodID]\" and sales_id=\"$_POST[salesID]\" ";
$result1 = mysql_query ($query1);
if(!$result1) error_message (sql_error());
$NumRows2 = mysql_num_rows($result1);
if(!$NumRows2)
{
$query7 = "insert into sales_detail values('$_POST[salesID]', '$_POST[prodID]', '$_POST[quantity]', $price) ";
$result7 = mysql_query ($query7);
if(!$result7) error_message (sql_error());
}
display( );
}
}
function display( )
{
$query8 = "select prod_id, quantity, sales_price
from sales_detail
where sales_id= \"$_POST[salesID]'\" ";
$result8= mysql_query ($query8);
if(!$result8) error_message (sql_error());
$NumRows = mysql_num_rows($result8);
while($row8 = mysql_fetch_array($result8))
{
$prod_id [ ]= $row8["prod_id"];
$quantity [ ]= $row8["quantity"];
$sales_price [ ]= $row8["sales_price"];
}
echo "<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">
<tr align=\"center\" bgcolor=\"gainsboro\">
<th>No.</th>
<th>Product ID</th>
<th>Quantity</th>
<th>Price </th>
<th>Subtotal</th>
</tr>";
$num=1;
$total=0;
for($i=0; $i<$NumRows; $i++)
{ $subtotal=$sales_price[$i]*$quantity[$i];
$total += $subtotal;
$subtotal = number_format($subtotal, 2);
echo "<tr align=\"center\" bgcolor=\"beige\">
<td>$num</td>
<td>$prod_id[$i]</td>
<td>$quantity[$i]</td>
<td>$sales_price[$i]</td>
<td>$subtotal</td>
</tr>";
$num++;
}
echo "</table><br>";
$total = number_format($total, 2);
echo "Total (RM) :         &nbs p   <b>$total</b><br>";
echo "Amount Tendered :   <input type=\"text\" name=\"pay\">";
echo "<input type=\"hidden\" name=\"Total\" value=\"$total\">";
echo "   <input type=\"submit\" name=\"btncalculate\" value=\"Calculate\"><br>";
}
function sales( )
{
echo "<table width=\"100%\">
<tr>
<td>Sales ID</td><th> $_POST[salesID]</th>
<td align=\"right\">Sales Date</td><th> $_POST[date]</th>
<td align=\"right\">Employee ID</td><th> $_POST[empID] </th>
<td align=\"right\">Customer ID</td><th> $_POST[custID] </th>
</tr>
</table>";
echo "Product ID";
$query3= "select prod_id from product";
$result3= mysql_query ($query3);
if(!$result3) error_message (sql_error());
$NumRows = mysql_num_rows($result3);
while($row3 = mysql_fetch_array($result3))
$prod_id[ ] = $row3["prod_id"];
echo " <select name=\"prodID\">";
for($i=0; $i<$NumRows; $i++)
{
echo "<option value=\"$prod_id[$i]\" >$prod_id[$i] </option>";
}
echo " </select>  ";
echo "Quantity<input type=\"text\" name=\"quantity\" size=\"5\" onBlur=\"validate(this)\">  ";
echo "<input type=\"hidden\" name=\"salesID\" value=\"$_POST[salesID]\">
<input type=\"hidden\" name=\"date\" value=\"$_POST[date]\">
<input type=\"hidden\" name=\"empID\" value=\"$_POST[empID]\">
<input type=\"hidden\" name=\"custID\" value=\"$_POST[custID]\">";
echo "<input type=\"submit\" name=\"addprod\" value=\"Add Product\" onclick=\"check(document.salestransactionform.quan tity.value)\">
<input type=\"submit\" name=\"deleteprod\" value=\"Delete Product\">
<input type=\"submit\" name=\"editprod\" value=\"Edit Quantity\">
<br><br>";
}
?>
<SCRIPT>
if (document.all)
{
loadMenus();
}
</SCRIPT>
</form>
</body>
<html>