SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question auto calculation

    Hi,

    I have a form having the following fields:

    1) Item Order (values are automatically picked up from a previous form)
    2) Item Name (values are automatically picked up from a previous form)
    3) Quantity (values are automatically picked up from a previous form)
    4) Unit Price (which will be manually inputed at this stage)
    5) Total (inputed manually at this stage as well)

    I just want to ask for help regarding about the possibilty / way of having the "Total" to be automatically calculated? Like Quantity multiplied by the Unit Price then the total values will appear on the input field "Total" automatically.

    Attached herewith are my current running form and data input files. Any help will be much appreciated.

    FORM:

    PHP Code:
    <form name="frmAddQuote" method="post" action="<?echo $g_base_url;?>/index.php?whattodo=viewjobs">
    <input type="hidden" name="t_id" value="<? echo $t_id?>">
    <table width="80%"  border="0" cellspacing="0" cellpadding="3" align=center>
    <?
    include("includes/functions.inc.php");

    $query "SELECT * FROM `ticketitems` Where t_id=$t_id";
    $mysqlresult query($query);
    $supplier_names="";

    //
    $supplier_ids implode(',',$_REQUEST['lstSupplier']);
    $supplier_names str_replace($suppliers_array_id,$suppliers_array_name,$supplier_ids);


    for(
    $i=0;$i<sizeof($_REQUEST["lstSupplier"]);$i++) {
    /*
    $supQuery = "Select supplier_name From supplier Where supplier_name='".$_REQUEST["lstSupplier"][$i]."'";
        //echo "<tr><td>".$supQuery."</td></tr>";
        $supResult = query($supQuery);
        $supRow = mysql_fetch_array($supResult);

        $supplier_ids .= $supRow["supplier_id"];
        $supplier_names .= $_REQUEST["lstSupplier"][$i];
        if($i<(sizeof($_REQUEST["lstSupplier"])-1)) {
            $supplier_ids .= ",";
            $supplier_names .= ",";
        }
    */

    ?>
      <tr>
        <td class="defaultfont">Quote information for ticket # <? echo $t_id?> from supplier <?= str_replace($suppliers_array_id,$suppliers_array_name,$_REQUEST["lstSupplier"][$i]) ?></td>
      </tr>
      <tr>
        <td class="defaultfont">&nbsp;</td>
      </tr>
      <tr>
        <td class="defaultfont">
        <table width="80%"  border="0" cellspacing="0" cellpadding="3" >
          <tr>
            <td width="5%" class="defaultfont">No</td>
            <td width="30%" class="defaultfont">Name</td>

            <td width="30%" class="defaultfont">Quantity</td>
            <td width="13%" class="defaultfont">Unit Price</td>
          </tr>
          <?  while ($row mysql_fetch_array($mysqlresult,MYSQL_BOTH)) { ?>
          <tr>
            <td class="defaultfont"><? echo $row["ItemOrder"]; ?></td>
            <td class="defaultfont"><? echo $row["ItemName"]; ?></td>
            <td class="defaultfont"><? echo $row["ItemQty"]; ?></td>
            <td class="defaultfont"><input class=defaultfont name="ItemPrice_<?= $_REQUEST["lstSupplier"][$i]."_".$t_id ?>" type="text"></td>
          </tr>
          
          <? }
          
    mysql_data_seek($mysql_result,0)?>
          
          <tr>
          <td></td>
          <td></td>
          <td align="right" class="defaultfont">Total:</td>
          <td><input class=defaultfont name="ItemTotal_<?= $_REQUEST["lstSupplier"][$i]."_".$t_id ?>" type="text"></td>
          
          </tr>
        </table></td>
      </tr>
      <tr>
        <td class="defaultfont">No. of. Days for Delivery as per quotation:
             <input class=defaultfont name="DeliveryDays_<?= $_REQUEST["lstSupplier"][$i?>" type="text" size="3" maxlength="3" >
          Days  </td>
      </tr>
       <? ?>
      <tr>
        <td height="19" class="defaultfont"><input type="hidden" name="lstSupplierIds[]" class=defaultfont value="<? echo $supplier_ids?>"><input type="hidden" name="lstSupplier[]" value="<? echo $supplier_names ?>"></td>
      </tr>
      <tr>
        <td height="19" class="defaultfont"><input name="cmdAddQuoteDetails" type="submit" class=defaultfont id="cmdAddQuoteDetails" value="Add Quote Details"></td>
      </tr>
     </table>
    </form>
    Data Input File:

    PHP Code:
    <?
        
    include("includes/functions.inc.php");
        
            
        for(
    $i=0;$i<sizeof($lstSupplierIds);$i++) {
            
    $itemQuery "SELECT * FROM `ticketitems` Where t_id=$t_id";
            
    $itemResult query($itemQuery);
            
            while(
    $itemRows mysql_fetch_array($itemResult)) {
                    
        
                
    $itemInsertQuery "Insert Into `itemquotedetails` (Id,Item_Id,Supplier_Id,Price, Total) Values (NULL, ".$itemRows["id"].",".$lstSupplierIds[$i].",".$_REQUEST["ItemPrice_".$lstSupplierIds[$i]."_".$t_id].",".$_REQUEST["ItemTotal_".$lstSupplierIds[$i]."_".$t_id].")";
    //            echo $itemInsertQuery."<br>";
                
    $itemInsertResult query($itemInsertQuery);
            }
            
            
    $deliveryDateQuery "Update itemquotedetails Set DeliveryDays=".$_REQUEST["DeliveryDays_".$lstSupplierIds[$i]]." Where Supplier_Id=".$lstSupplierIds[$i]." And Item_Id In (Select id from ticketitems where t_id=".$t_id.")";
            
    $res query($deliveryDateQuery);
        }
    ?>
    Last edited by cyp; Jun 10, 2005 at 23:28. Reason: edited code

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do the math in PHP then echo out the value in the "value" attribute of the input field. It seems from reading some of your code that your already know how to do that. So maybe I am not understanding your question.

    Maybe you want the total field to get updated when the user changes values in the other fields, without having them refresh the page. In that case, Javascript is your best solution. Just remember to validate the fields using PHP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •