SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Live javascript calculating

    Hi there!

    I hope one of you javascript experts can help me out on this one. In html and PHP I made a purchase script. Al the products are listed inside a WHILE loop. The problem is, when I POST it starts entering data in the database. I would like the possibility that when I enter a number in the "aantal" field that it instantly calculates live how much it is going to cost.

    So, "aantal$i" * inkoop_prijs = Subtotal
    And, all Subtotals together = Total

    Big respect to the one that solves this

    Code:
    	<?php 
    		if(isset($leverancier_id)) { 
    		$i = 1;
    		$totaal = 0;
    	
    			while($prodinfo = mysql_fetch_assoc($prodres)) {  ?>
    	<tr>	
    		<td><?=$prodinfo['product_id'];?></td>
    		<td><?=$prodinfo['naam'];?></td>
    		<td align="center"><input name="aantal<?=$i;?>" type="text" value="<?=$_POST['aantal'.$i];?>" size="5" /></td>
    		<td><?=number_format($prodinfo['inkoop_prijs'], 2);?></td>	
    		<td align="right">
    		<input type="hidden" name="product_id<?=$i;?>" value="<?=$prodinfo['product_id'];?>" />
    		<input type="hidden" name="inkoop_prijs<?=$i;?>" value="<?=$prodinfo['inkoop_prijs'];?>" />Subtotal</td>
    	</tr>
    	<?php 
    	$i++;
    	} }
    	?>
    
    	<tr>
    		<td colspan="2" valign="top" align="right"><strong>Total:</strong></td>
    		<td colspan="2" valign="top">incl. 19&#37; BTW </td>
    		<td align="right"><strong>&euro; </strong></td>
    	</tr>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    var aantals = document.getElementById('product_info').getElementsByTagName('input'), total = document.getElementById('total'), prijs, subtotal;
    for (var i = 0, j = aantals.length; i < j; i++) {
      if (aantals[i].name.indexOf('aantal') === 0) {
        prijs = aantals[i].parentNode.nextSibling.nextSibling.firstChild.value;
        subtotal = prijs * aantals[i].value;
      }
      else if (aantals[i].name.indexOf('inkoop_prijs') === 0) {
        aantals[i].value = subtotal;
        total.firstChild.nodeValue = parseInt(total.firstChild.nodeValue, 10) + subtotal;
      }
    You'll need to change the <strong>Total:</strong> to:
    HTML Code:
    <strong>Total: <span id="total"></span></strong>
    And the table that contains all this stuff will need to have an ID of product_info (or change the ID above).

    I suspect you should not be using a table for this as it doesn't really seem like tabular data. Are you working with a CMS from the 1990s?


Bookmarks

Posting Permissions

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