Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2007
    Location
    malaysia
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is it possible? [calculator]



    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.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Yeah, use javascript. This kind of does what you're looking for I think.

    Code:
    <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>

  • #3
    New Coder
    Join Date
    Jul 2007
    Location
    malaysia
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ohh.. thank you ralph.. but what if i take value from database?

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Code:
    <?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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •