lional Posted September 10, 2007 Share Posted September 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
micah1701 Posted September 10, 2007 Share Posted September 10, 2007 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> Quote Link to comment Share on other sites More sharing options...
lional Posted September 10, 2007 Author Share Posted September 10, 2007 Thanks that is great. I am fairly new to javascipt, so for one final question. Is it possible to combine php with javascipt. I want to pull the rate from a mysql database and use javascipt to calculate on the php variable Thanks Lional Quote Link to comment Share on other sites More sharing options...
micah1701 Posted September 10, 2007 Share Posted September 10, 2007 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> Quote Link to comment Share on other sites More sharing options...
werny Posted September 12, 2007 Share Posted September 12, 2007 how would it look if you have multiple quantities, with different rates? Quote Link to comment Share on other sites More sharing options...
werny Posted September 12, 2007 Share Posted September 12, 2007 nvm i got Quote Link to comment Share on other sites More sharing options...
micah1701 Posted September 12, 2007 Share Posted September 12, 2007 nevermind i got awesome! good job Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.