Hi there, I am a PHP developer and know little of JS - in my current project I have hit a few probs as I know I need to incoporate some JS. After some help already from this community I have almost completed the JS part of one page that totals without needing a refresh.
I have looked up JS resources that show me how easy it is to take a value and make it and change it to 2 dec places. However, I can not seem to put the relevant code into the function - a very scaled down version of the function is
<script language="JavaScript">
function sumtot()
{
var unit_price, qty, tot;
unit_price = document.box.unit_price.value;
qty = document.box.qty.value;
tot = document.box.tot.value;
document.box.tot.value = (unit_price * qty);
}
</script>
In reality all the the JS is created using PHP dependant upon the items on the page and the unit_price is pulled from a DB Table and the qty is entered for each by the visitor.
What do I need to do to a) format sumtot() to [0.00] to 2.d.p and b) if you have time, the final issue, I would like to ensure that dependant upon the unit[each or kilo] that inputs into the qty field can only accept whole integers and 1 d.p. respectively.
If it helps, I did not want to clog up the start of the post with a load of mark-up, below is an example of the XHTML generated by my PHP/JS - many thanks in advance for any assistance - A
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="http://www.slipstream-organics.co.uk/css.css" />
<meta name="author" content="" />
<meta name="description" content="" />
<meta name="keywords" content="" /> <script language='JavaScript'>function sumtot() {var unit_price_8, qty_8, unit_price_9, qty_9, unit_price_10, qty_10, unit_price_12, qty_12, tot;unit_price_8=document.box.unit_price_8.value; unit_price_9=document.box.unit_price_9.value; unit_price_10=document.box.unit_price_10.value; unit_price_12=document.box.unit_price_12.value; qty_8 = document.box.qty_8.value; qty_9 = document.box.qty_9.value; qty_10 = document.box.qty_10.value; qty_12 = document.box.qty_12.value; document.box.tot.value = (unit_price_8 * qty_8) + (unit_price_9 * qty_9) + (unit_price_10 * qty_10) + (unit_price_12 * qty_12) + 0;}</script>
</head>
<body>
<img src="../images/logo.gif" width="400" height="47">
<table style='width:400px;'>
<tr>
<td colspan='2' style='color:#363;font-weight:bold;text-align:left;line-height:1.5em;font-family:verdana, helvetics, sans-serif;'>Your Details</td>
</tr>
</table>
<table style='width:400px;background:rgb(230,240,230);'>
<tr>
<td><p class='header'>Name: </p></td><td><p class='header'> PLANT</p></td>
</tr>
<td style='vertical-align:top;'><p class='header'><p class='header'>Address: </p></td>
<td><p class='header'>2 John Daniels Way<br />Churchdown<br />Glos<br />GL3 2QY</p></td>
</tr> <tr>
<td><p class='header'>Delivery Round: </p></td><td><p class='header'>Gloucester1</p></td></tr></table><br /><p class='header' style='color:#800;'>THIS ORDER WILL <b>REPLACE</b> YOUR ORDER <b>THIS</b> WEEK</p><p>Some text here to explain what to do, Nick...</p><form method='POST' name='box' action='order.php'><table style='color:#363;font-size:85%;font-family:verdana, helvetica, sans-serif;'>
<tr>
<td><b>Product</b></td>
<td style='width:50px;text-align:left;'><b>Unit</b></td>
<td style='text-align:right;'><b>Price</b></td>
<td><b>Qty</b></td>
<td><b>Line Total</b></td>
</tr><tr>
<td>Potatoes</td>
<td>Kilo</td>
<td>£0.66</td><input type='hidden' name='unit_price_8' value='0.66' />
<td><input type='text' size='4' name='qty_8' /><td>
<td>ln tot</td>
</tr><tr>
<td>Carrot</td>
<td>Kilo</td>
<td>£0.84</td><input type='hidden' name='unit_price_9' value='0.84' />
<td><input type='text' size='4' name='qty_9' /><td>
<td>ln tot</td>
</tr><tr>
<td>Onions</td>
<td>Kilo</td>
<td>£1.60</td><input type='hidden' name='unit_price_10' value='1.60' />
<td><input type='text' size='4' name='qty_10' /><td>
<td>ln tot</td>
</tr><tr>
<td>Cauliflower</td>
<td>Each</td>
<td>£1.20</td><input type='hidden' name='unit_price_12' value='1.20' />
<td><input type='text' size='4' name='qty_12' /><td>
<td>ln tot</td>
</tr></table><input type='submit' value='Send Order' /><input name='tot' type='text' /><input type='button' value='Calculate' onClick='sumtot()'></form>
</body>
</html>