Jump to content

Calculation question


lional

Recommended Posts

Hi All

Quick Javascript qusetion.

I have a form with a product name, quantity box, rate and line total. Is it possible that javascipt calculates the quantity multiplied by the rate and puts the line total in a box without submitting the form. So if I say put 2 in the quantity box and then tab to the next box, and say the rate is 2, can it then put 4 in the line total box without submitting the form

 

Thanks

 

Lional

Link to comment
https://forums.phpfreaks.com/topic/68681-calculation-question/
Share on other sites

yes that is possible.

something like:

<script type="text/javascript">
function calcCost(){
var quantity = document.getElementById('quantity').value;
var rate = 10;
var total = quantity*rate;
document.getElementById('lineTotal').innerHTML = total;
}
</script>

<body>
<table border="0">
  <tr>
    <td><input name="quantity" id="quantity" type="text" onkeyup="calcCost()" />(quantity)</td>
    <td>$10.00 (rate)</td>
    <td><div id="lineTotal">total rate</div></td>
  </tr>
</table>

 

 

Link to comment
https://forums.phpfreaks.com/topic/68681-calculation-question/#findComment-345302
Share on other sites

well the php has to run server side, before the page loads (where as javascript runs client side, after the page loads in the browser)

 

so you CAN do what your asking:

 

<?php
//some code to calculate the rate
$rate = 10.00;
?>

<script type="text/javascript">
function calcCost(){
var quantity = document.getElementById('quantity').value;
var rate = <?php echo $rate ?>;
var total = quantity*rate;
document.getElementById('lineTotal').innerHTML = total;
}
</script>


Link to comment
https://forums.phpfreaks.com/topic/68681-calculation-question/#findComment-345351
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.