Hi. I want to auto calculate some fields in database without any button submit but i'm only know how to calculate using button submit. I want that user enter the value then the user will get the calculate value automatically without click on button submit. I'm doing my final year project. Any help would be greatly appeciated. Thank you. :)

<form action="laporan_kewangan.php" method="post">

<?php 
 include ('link_db.php'); 
?>

    <?php
error_reporting(E_ALL & ~E_NOTICE);

$no1=$_POST['peruntukan_diluluskan'];
$no2=$_POST['jumlah_perbelanjaan'];
$tolak5=$_POST['baki'];

$tolak5=@($no1-$no2);
?>
<table width="800" border="0" align="center">
          <tr>
            <td width="30"><strong>1</strong></td>
            <td width="170"><span class="gh"><strong>Peruntukan diluluskan (Approved budget)</strong></span><strong> </strong></td>
            <td width="10">:</td>
            <td width="472"><?php echo $record["peruntukan_diluluskan"]; ?><input name="peruntukan_diluluskan" type="hidden" id="peruntukan_diluluskan" readonly ="readonly" value="<?php echo $record["peruntukan_diluluskan"]; ?>" size="50" /></td>
          </tr>
          <tr>
            <td width="30"><strong>2</strong></td>
            <td width="170"><span class="hb"><span class="hb"><strong>Jumlah Perbelanjaan (Amount Spent) </strong></span></span><strong></strong></td>
            <td width="10">:</td>
            <td width="472"><?php echo $record["jumlah_perbelanjaan"]; ?><input name="jumlah_perbelanjaan"type="hidden" id="jumlah_perbelanjaan" readonly ="readonly"  value="<?php echo $record["jumlah_perbelanjaan"]; ?>" size="30" /></td>
          </tr>
           <tr>
            <td width="30"><strong>3</strong></td>
            <td width="170"><span class="hb"><span class="hb"><strong>Baki (Balance) </strong></span></span><strong></strong></td>
            <td width="10">:</td>
            <td width="472"><?php  echo $tolak5;
 ?><input type="hidden" name="baki" id="baki" value="<?php  echo $tolak5;
 ?>" size="50" /></td>
          </tr>

You can do AJAX calls on every key up event if you want it to be that responsive, that'll send a request to the server for partial amounts as well because it won't know when the user has finished typing.
Or you can call when the input uses focus if that is relevant.

Member Avatar for RudyM

If you've got the field values from the database to your front-end, why not just focus the calculations in the front-end with the key press events? It would mean a quicker response. I wouldn't recommend AJAX calls for each key press event; it may stress your db depending. But, maybe it's what you're project requires?

Member Avatar for red.peugeot

So how does the system know when the data entered is completed?
What's the "trigger"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.