0

I have a very simple website which helps me to calculate the end of day cash position in my store. Form is here: http://www.073design.nl/kasopmaak/kasopmaak.php#

But now I have to 'click' the "Calculate cash!" button to update the positions.

My questions now; is it possible to add some 'smart' extra code, which updates the totals directly after entering data per line?

Many thanks in advance.

Form uses only this code:

<?php

if(isset($_POST)) {

$total_100  = $_POST['100']  * 100     ;
$total_50   = $_POST['50' ]  *  50     ;
$total_20   = $_POST['20' ]  *  20     ; 
$total_10   = $_POST['10' ]  *  10     ;
$total_5    = $_POST['5'  ]  *   5     ;
$total_2    = $_POST['2'  ]  *   2     ;
$total_1    = $_POST['1'  ]  *   1     ;
$total_05   = $_POST['05' ]  *   0.5   ; 
$total_02   = $_POST['02' ]  *   0.2   ; 
$total_01   = $_POST['01' ]  *   0.1   ;
$total_005  = $_POST['005']  *   0.05  ;

$total = $total_100 + $total_50 + $total_20 + $total_10 + $total_5;  

} else {

$total_100  = 0  ;
$total_50   = 0  ;
$total_20   = 0  ; 
$total_10   = 0  ;
$total_5    = 0  ;
$total_2    = 0  ;
$total_1    = 0  ;
$total_05   = 0  ; 
$total_02   = 0  ;
$total_005  = 0  ;

$total = $total_100 + $total_50 + $total_20 + $total_10 + $total_5 + $total_2 +  $total_1 +  $total_05 +  $total_02 +  $total_01 +  $total_005 ;  

}

?>


<body>

<h1>kas opmaak</h1>

<form action="#" method="POST">
<table>
            <tr>
                <td><input type="number" name="100" value="<?php echo $_POST['100']; ?>"></td>
                <td>X</td>
                <td>&#8364; 100</td>
                <td> = </td>
                <td align="right"><?php echo $total_100;?></td>
            </tr>
            <tr>
                <td><input type="number" name="50"  value="<?php echo $_POST['50'];?>"></td>
                <td>X</td>
                <td>&#8364; 50</td>
                <td> = </td>
                <td align="right"><?php echo $total_50;?></td>
            </tr>
            <tr>
                <td><input type="number" name="20"  value="<?php echo $_POST['20'];?>"></td>
                <td>X</td>
                <td>&#8364; 20</td>
                <td> = </td>
                <td align="right"><?php echo $total_20;?></td>
            </tr>
            <tr>
                <td><input type="number"  name="10" value="<?php echo $_POST['10'];?>"></td>
                <td>X</td>
                <td>&#8364; 10</td>
                <td> = </td>
                <td align="right"><?php echo $total_10;?></td>
            </tr>           
            <tr>
                <td><input type="number"  name="5"  value="<?php echo $_POST['5'];?>"></td>
                <td>X</td>
                <td>&#8364; 5</td>
                <td> = </td>
                <td align="right"><?php echo $total_5;?></td>
            </tr>
<tr>
                <td><input type="number"  name="2"  value="<?php echo $_POST['2'];?>"></td>
                <td>X</td>
                <td>&#8364; 2</td>
                <td> = </td>
                <td align="right"><?php echo $total_2;?></td>
            </tr>
<tr>
                <td><input type="number"  name="1"  value="<?php echo $_POST['1'];?>"></td>
                <td>X</td>
                <td>&#8364; 1</td>
                <td> = </td>
                <td align="right"><?php echo $total_1;?></td>
            </tr>
<tr>
                <td><input type="number"  name="05"  value="<?php echo $_POST['05'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,50</td>
                <td> = </td>
                <td align="right"><?php echo $total_05;?></td>
            </tr>
<tr>
                <td><input type="number"  name="02"  value="<?php echo $_POST['02'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,20</td>
                <td> = </td>
                <td align="right"><?php echo $total_02;?></td>
            </tr>
<tr>
                <td><input type="number"  name="01"  value="<?php echo $_POST['01'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,10</td>
                <td> = </td>
                <td align="right"><?php echo $total_01;?></td>
            </tr>
            <tr>
                <td><input type="number"  name="005"  value="<?php echo $_POST['005'];?>"></td>
                <td>X</td>
                <td>&#8364; 0,05</td>
                <td> = </td>
                <td align="right"><?php echo $total_005;?></td>
            </tr>           
            <tr>
                <td colspan="4"><strong> Totaal contant </strong></td>

                <td><?php echo $total;?></td>
            </tr>               








            <tr>
                <td colspan="5" align="right"><br/>  <input type="submit" value="Calculate cash!"></td>

            </tr>

</table>        
</form>
4
  • was that you wanted ? Commented May 3, 2015 at 17:25
  • No, not exactly. I want that after someone enters for example '10' at EUR 50,- directly the line is updatet with 10 x 50 = 500. So you don't have to push enter meanwhile you're filling out the form Commented May 3, 2015 at 17:30
  • u need ajax for that , i dont know much of it may someone else will help you Commented May 3, 2015 at 17:33
  • Thanks Wasim! Fingers crossed for the next helping hand. Enjoy your weekend. Commented May 3, 2015 at 17:35

1 Answer 1

1

It is relatively easy to do this using jquery. I wrote for you a sample code by implement only your first formula [ ] X 100 = [ ]. It can be very easy adapted to cover all your formulas.

When the user enters a number the formula is automatically calculated. It also works with the backspace or delete when the user deletes a number.... Please note that for the case simplicity I avoided to use any error-exception handling code. It also works without the need of server... ;)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
i = 0;
$(document).ready(function(){  
   $("#in100").keyup(function(){
      $('#out100').get(0).value = $('#in100').get(0).value * 100;
    });
 });
</script>
</head>
<body>

<input type="text" id="in100" /> X 100 = <input type="text" id="out100" readonly="true" value=""/>


</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.