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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.