Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2004, 09:28 PM
Authorized User
 
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default function that doesn't work

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) :&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbs p&nbsp&nbsp <b>$total</b><br>";
    echo "Amount Tendered : &nbsp <input type=\"text\" name=\"pay\">";
    echo "<input type=\"hidden\" name=\"Total\" value=\"$total\">";
    echo "&nbsp&nbsp&nbsp<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>&nbsp&nbsp";
    echo "Quantity<input type=\"text\" name=\"quantity\" size=\"5\" onBlur=\"validate(this)\">&nbsp&nbsp";
    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>

 
Old March 10th, 2004, 03:20 AM
Authorized User
 
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No need liao guys. I've solve it.i forgot to return the result.






Similar Threads
Thread Thread Starter Forum Replies Last Post
TRIM, ISTEXT function won't work in Excel VBA code munzer1 Excel VBA 1 May 29th, 2007 09:35 AM
javaScript function in asp.net not work in firefox rsanuj ASP.NET 1.x and 2.0 Application Design 1 February 8th, 2007 11:37 AM
mail function doesn't work ashraf_gawdat Beginning PHP 2 January 29th, 2007 07:21 AM
tabindexes don't work with onBlur function in Net elisabeth Javascript 14 November 24th, 2004 11:07 AM
can't seem to get the mail() function to work Graham76 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 5th, 2004 01:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.