elgoog Posted January 25, 2009 Share Posted January 25, 2009 First off. Thanks if you take the time to read this.... quite long I have a script that displays products ordered. It shows the product name and the amount ordered. I want to be able to calculate a running total of the amount that needs charging the customer based on available stock being input in the following way: If the quantity ordered is only 1, a checkbox will display. If there is more than one. A dropdown will display with the the max options of total ordered for that product. Example of one of the orders Product Name | Quantity Ordered | Quantity picked Product 1 3 echo "<select name=\"Username[$i]\">"; echo "<option value=\"".$row2['final_price']."\">1</option>"; echo "<option value=\"".$row2['final_price'] * 2."\">2</option>"; echo "<option value=\"".$row2['final_price'] * 3."\">3</option>"; echo "</select> [/td] [td]Product 2 1 echo "<input name=\"Username[$i]\" type=\"checkbox\" value=\"".$row2['final_price']."\" onClick=\"UpdateCost()\">"; Now i have found the some scripts on the internet for calculating running totals and autosums, but because of my limited javascript knowledge. Cannot figure out how to do the following(using an example i found of javascript below) 1. the name of my fields are Username[$i] ($i - replaced in php by number)and not game. No idea on how i go about changing this 2. I need this to be able to work with a checkbox and also a dropdown selection. 3. There will be a variable number of products. Not a max of 5 as in the javascript example below <script type="text/javascript"> function UpdateCost() { var sum = 0; var gn, elem; for (i=0; i<5; i++) { gn = 'game'+i; elem = document.getElementById(gn); if (elem.checked == true) { sum += Number(elem.value); } } document.getElementById('totalcost').value = sum.toFixed(2); } </script> <input type="checkbox" id='game0' value="9.99" onclick="UpdateCost()">Game 1 ( 9.99)<br> <input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br> <input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br> <input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br> <input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br> <input type="text" id="totalcost" value=""> If you know of any good examples or have done anything similar. Hoping i can get some ideas to help out. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 26, 2009 Share Posted January 26, 2009 <html> <head> <script type="text/javascript"> function UpdateCost() { var sum = 0; var elem; var i = 0; while (document.getElementById('game'+i)) { elem = document.getElementById('game'+i); if (elem.type=='checkbox') { sum += (elem.checked) ? Number(elem.value) : 0; } else if (elem.type=='select-one') { sum += Number(elem.options[elem.selectedIndex].value); } i++; } document.getElementById('totalcost').value = sum.toFixed(2); } // </script> </head> <body> <input type="checkbox" id='game0' value="9.99" onclick="UpdateCost()"> Game 1 ( 9.99)<br> <input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()"> Game 2 (19.99)<br> <input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()"> Game 3 (27.50)<br> <input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()"> Game 4 (45.65)<br> <input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()"> Game 5 (87.20)<br> Game 6 <select name="" id="game5" onchange="UpdateCost()"> <option value="0">0</option> <option value="9.99">1 (9.99)</option> <option value="19.98">2 (19.98)</option> <option value="29.97">3 (29.97)</option> </select><br> Total <input type="text" id="totalcost" value=""> </body> </html> 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.