Problem in Subtraction

Good day!

I need to automatically subtract the input qty and output qty and the result should be appear in the text area of qty.

the input qty and output qty depend on the operation name.

i try this code:


<?php
  include("config.php");
  
  $query = "SELECT operation_name FROM clt_traceability WHERE operation_name = '01. Spreading'";
  
  $inqty = $_POST['input_qty'];
  $outqty = $_POST['output_qty'];
  $qty = $_POST['variance_qty'];
  
  $qty = $inqty - $outqty;

  
?>
<html>
<body>
Input<input type = 'text' name='inqty' id='inqty' />
Output<input type = 'text' name='outqty' id='outqty' />
Qty<input type = 'text' name='qty' id='qty' />
</body>
</hmtl> 

in this code when i enter input = 100 output= 50 and when I enter or I used tab no output in qty.

Thank you…

I think it should be something like this:


<?php 
    //include("config.php"); 
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        //$query = "SELECT operation_name FROM clt_traceability WHERE operation_name = '01. Spreading'"; 
        $inqty = $_POST['inqty']; 
        $outqty = $_POST['outqty']; 
        $qty = $inqty - $outqty;
    }
?> 
<html> 
    <body> 
        <form method="post" name="frm1" id="frm1" action="">
        Input : <input type="text" name='inqty' id='inqty' value="<?php echo isset($inqty) ? $inqty : ''; ?>" /> 
        Output : <input type="text" name='outqty' id='outqty' value="<?php echo isset($outqty) ? $outqty : ''; ?>" /> 
        Qty : <input type="text" name='qty' id='qty' value="<?php echo isset($qty) ? $qty : ''; ?>" />
        <input name="btnsubmit" id="btnsubmit" value="Calculate" type="submit" />
        </form> 
    </body> 
</html>

you are overwriting the $qty sent in $_POST[‘variance_qty’] with the value of $qty calculated in $qty = $inqty - $outqty.

are you sure you really want to do this?

and what is the purpose of the sql query in your code? I don’t see it used anywhere.

As I’ve on my first thread, that code is for testing purposes only. Now I will post the real code of my webpage which I need to add code for subtracting input qty and output qty and the result is automatically display in qty text field.


<?php
include("config.php");
$rexist = 0;
if($_POST["no"])
    {
    $clt_date = $_POST["date"];
    $query = "INSERT INTO clt (no, ac2l, b3, type, roll, t_date, date) VALUES ('" . $_POST["no"] . "', '" . $_POST["ac2l"] . "', '" . $_POST["b3"] . "', '" . $_POST["type"] . "', '" . $_POST["roll"] . "', now(), '" . $date . "')";
    $result = mysql_query($query);
    $transact_id = mysql_insert_id();
    
    $opname = $_POST["opname"];
    $timein = $_POST["timein"];
    $timeout = $_POST["timeout"];
    $inqty = $_POST["inqty"];
    $iun = $_POST["iun"];
    $outqty = $_POST["outqty"];
    $oun = $_POST["oun"];
    $idno = $_POST["idno"];
    $mcno = $_POST["mcno"];
    $varqty = $_POST["varqty"];
    $varsublot = $_POST["varsublot"];
    $dateshift = $_POST["dateshift"];
    $shift = $_POST["shift"];
    
    $totalarr = count($opname) - 1;
    for($ctr=0; $ctr < $totalarr; $ctr++)
        {
        $inqty[$ctr] = (float) $inqty[$ctr];
        $varqty[$ctr] = (float) $varqty[$ctr];
        $outqty[$ctr] = (float) $outqty[$ctr];
        $query = "INSERT INTO traceability (operation_name, time_in, time_out, input_qty, input_unit, output_qty, output_unit, id_no, mc_no, variance_qty, variance_sublot, date, shift, transact_id) VALUES ('" . $opname[$ctr] . "', '" . $timein[$ctr] . "', '" . $timeout[$ctr] . "', '" . $inqty[$ctr] . "' , '" . $iun[$ctr] . "' ,'" . $outqty[$ctr] . "', '" . $oun[$ctr] . "' , '" . $idno[$ctr] . "', '" . $mcno[$ctr] . "', '" . $varqty[$ctr] . "', '" . $varsublot[$ctr] . "', '" . $dateshift[$ctr] . "', '" . $shift[$ctr] . "', '" . $transact_id . "')";
        $result = mysql_query($query);
        
        }   
    }
$rexist = 0;
?>
<html>
<head>
    <script language="javascript">
    function handleEnter(e, nextfield)
        {
        var characterCode = (e && e.which)? e.which: e.keyCode;
        if(characterCode == 13)
            {
            document.getElementById(nextfield).focus();
            return false;
            } 
        else
            {
            return true;
            }
        }
    </script>
</head>
<body onLoad="document.type.no.focus();">
<!--<p><a href="definition.php">Types</a><a href="query.php">Query</a></p>-->
<?php
include("menu.php");
?>
<form action="main.php" method="post" name="type">
<?php
    $rexist = 1;
    echo "<p>No:<input type='text' name='_no' onkeypress='return handleEnter(event,\\"date\\");' /><br />";
    echo "Date:;<input type='text' name='date' onkeypress='return handleEnter(event,\\"ac2l\\");' /></p>";
    
    echo "<table>";
    echo "<tr><th class='myclass'>TRACEABILITY</th><th class='myclass'>LOT #</th></tr>";
    echo "<tr><td>AC2L</td><td><input type='text' name='ac2l' id='ac2l' onkeypress='return handleEnter(event,\\"b3\\");' /></td></tr>";
    echo "<tr><td>B3</td><td><input type='text' name='b3' id='b3' onkeypress='return handleEnter(event,\\"type\\");' /></td></tr>";
    echo "<tr><td>Type</td><td><input type='text' name='type' id='type' onkeypress='return handleEnter(event,\\"roll\\");' /></td></tr>";
    echo "<tr><td>Roll</td><td><input type='text' name='roll' id='roll' onkeypress='return handleEnter(event,\\"timein0\\");' /></td></tr>";
    echo "</table>";
    echo "<p>&nbsp;</p>";
    
    $query = "SELECT * FROM trace_operations ORDER BY operation_name";
    $last_operation_name = "";
    $result = mysql_query($query);
    if($result)
        {
        $rexist = 1;
        echo "<p><center><b>TRACEABILITY</center></b></p>";
        echo "<table>";
        echo "<tr><th class='myclass'>OPERATIONS</th><th class='myclass'>Time IN</th><th class='myclass'>Time OUT</th><th class='myclass'>INPUT <br/> QTY</th><th class='myclass'>UNIT</th><th class='myclass'>OUTPUT QTY</th><th class='myclass'>UNIT</th><th class='myclass'>NAME & ID #</th><th class='myclass'>MC #</th><th class='myclass'>VARIANCE Qty</th><th class='myclass'>VARIANCE Sub Lot</th><th class='myclass'>DATE</th><th class='myclass'>SHIFT</th></tr>";
        $totalrows = mysql_num_rows($result);
        $trows = $totalrows - 1;
        for($ctr = 0; $ctr < $trows; $ctr++)
            {
            $row = mysql_fetch_array($result);
            $tctr = $ctr + 1;
            echo "\
\	<tr>";
            echo "\
\	\	<td>";
            if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
            $last_operation_name = $row["operation_name"];
            echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";   
            echo "\
\	\	<td><input type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\\"timeout" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\\"inqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\\"outqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td>" . $row["input_unit"];
            echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
            echo "\
\	\	<td><input type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\\"idno" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td>" . $row["output_unit"];
            echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
            echo "\
\	\	<td><input type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\\"mcno" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\\"varqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='varsublot[]' id='varsublot" . $ctr . "' onkeypress='return handleEnter(event,\\"dateshift" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\\"shift" . $ctr . "\\");' /></td>";
           echo "\
\	\	<td><input type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\\"timein" . $tctr . "\\");' /></td>";
            echo "\
\	</tr>";
            }
        if($totalrows > 1);
            {
            $row = mysql_fetch_array($result);
            echo "\
\	<tr>";
            echo "\
\	\	<td>";
            if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
            $last_operation_name = $row["operation_name"];
            echo "<input type='hidden'  name='opname[]' value='" . $row["operation_name"] . "' /></td>";   
            echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";
            echo "\
\	\	<td><input  type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\\"timeout" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input  type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\\"inqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input  type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\\"outqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td>" . $row["input_unit"];
            echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
            echo "\
\	\	<td><input  type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\\"idno" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td>" . $row["output_unit"];
            echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
            echo "\
\	\	<td><input  type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\\"mcno" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\\"varqty" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='varqty[]' id='varqty" . $ctr . "' onkeypress='return handleEnter(event,\\"varsublot" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='varsublot[]' id='varsublot" . $ctr . "' onkeypress='return handleEnter(event,\\"dateshift" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\\"shift" . $ctr . "\\");' /></td>";
            echo "\
\	\	<td><input type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\\"saveform\\");' /></td>";
            echo "\
\	</tr>";
            }
        echo "</table>";
        }
    echo "<p><input type='submit' value='  Save  ' id='saveform' /></p>";
    echo "<input type='hidden' name='typeno' value='" . $_POST["typeno"] . "' />";
?>
</form>
</body>
</html>

I need to subtract input qty to output qty then the result is automatically display in qty text field, I want it to happen in the operation is 03. Spread and 03. Pack

in your 1st post you don’t mention the code there was for testing purposes only so I don’t know for sure if this is your “real” code or not.

Sorry, the new code that I post is the real code

ok if you say so, but I’m not so sure so I’ll pass on this one.

I attach my whole file

And I have the select statement code where only those operation name could subtract the input and output


SELECT t.operation_name FROM clt_traceability t, clt_transact c WHERE t.operation_name in ('operation_name1', 'operation_name3', 'operation_name4', 'operation_name5') AND t.clt_transact_id = c.clt_transact_id

my problem is where i can put this code in my php file and how thus the input and output subtract and automatic the varqty was appear as the result of subtration of input and output.

Thank you…

this codes:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type= "text/javascript"> 
function startCalc(){
  interval = setInterval("calc()",1);
    }
function calc(){
    var qty = new Array(2);
    qty[0] = parseFloat(document.myform.inqty.value);
  qty[1] = parseFloat(document.myform.outqty.value);
  
myqtyval = qty[0]-qty[1];
document.myform.myqty.value = myqtyval;    
}
 
function stopCalc(){
  clearInterval(interval);
}
</script>
</head>
<body>
<form name='myform' action='test.php' method='post'>
Input<input type='text' name='inqty'  onFocus='startCalc();' onBlur='stopCalc();' />
Output<input type='text' name='outqty'  onFocus='startCalc();' onBlur='stopCalc();' /><br/>
myqty<input type='text' name='myqty' value=''  />
<input type='submit' name='submit' value='save' />
 
 
</form>
</body>
</html>

has the concept of what i want that after i insert number in output the result was automatically appear in qty but the problem in this code it did not use on key enter and I don’t know how can i put that code n my php file where I attach.

I’m sorry, i do understand the concept but I don’t know how can I code it and where i can put the codes.

Thank you…

What do you mean by key enter? Do you mean the calculation should happen only when the Enter key is pressed? It seems the question related to JS.

on the file that i attach you can see that i have a javascript code for on enter. I used enter key to go to the another text field. I want is after I insert number in output the result will appear in varqty or after I press enter.

Thank you

The use the onKeyUp event instead of onBlur.

I used this code i change onBlur into onKeyUp

 
echo "\
\	\	<td><input size='6' type='text' name='inqty[]' onFocus='startCalc();' onKeyUp='stopCalc();'  id='inqty"  . $ctr . "' onkeypress='return handleEnter(event,\\"outqty" . $ctr . "\\");' /></td>";

But still did not work