...

View Full Version : is it possible? [calculator]



nurvirus
07-18-2007, 05:03 AM
http://i12.photobucket.com/albums/a208/n_amalyna/calculate.jpg

Hi everyone,
is it possible if i want to calculate the product * quantity with just scrolling and shows at the price without any button? If it is.. which method should i use? javascript?

Thank you in advance.

ralph l mayo
07-18-2007, 07:20 AM
Yeah, use javascript. This kind of does what you're looking for I think.



<head>
<script type="text/javascript">
/**
* Note the keys here relate directly to the values you use
* in the product selection dropdown.
*/
prices = {
'1' : 25.95,
'2' : 10
}

gebi = function(id) { return document.getElementById(id); }

window.onload = updatePriceDisplay = function()
{
gebi('total_price').innerText = (prices[gebi('product').value] * gebi('quantity').value).toFixed(2);
}
</script>
</head>
<body>
<p>
<select id="product" onchange="updatePriceDisplay()">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
</p>
<p>
<select id="quantity" onchange="updatePriceDisplay()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</p>
<p><span id="total_price" /></p>
</body>

nurvirus
07-18-2007, 08:09 AM
ohh.. thank you ralph.. but what if i take value from database?

rafiki
07-18-2007, 01:27 PM
<?php
//retrieve your prices..
?>

<head>
<script type="text/javascript">
/**
* Note the keys here relate directly to the values you use
* in the product selection dropdown.
*/
prices = {
'1' : <?php echo $row['price_1']; ?>,
'2' : <?php echo $row['price_2']; ?>
}

gebi = function(id) { return document.getElementById(id); }

window.onload = updatePriceDisplay = function()
{
gebi('total_price').innerText = (prices[gebi('product').value] * gebi('quantity').value).toFixed(2);
}
</script>
</head>
<body>
<p>
<select id="product" onchange="updatePriceDisplay()">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
</p>
<p>
<select id="quantity" onchange="updatePriceDisplay()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</p>
<p><span id="total_price" /></p>
</body>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum