Results 1 to 2 of 2

Thread: Calculating a total from hidden form field

  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Calculating a total from hidden form field

    I've written a script that I was hoping would automatically calculate a total. The script pulls a price, which is given from a database to a hidden form field. Then once the quantity is changed the bottom form field automatically updates the price. Here's my script if someone could possibly point out why this might not be working (or is my logic flawed.)
    Thanks in advance!

    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Shopping Cart</title>
    
    <script type="text/javascript">
    
    	/* <![CDATA[ */
    
    		function calcTotal(){
    		
    			var calcResult;
    			var hidden2;
    			var hidden3;
    			var hidden4;
    			var hidden5;
    			var hidden6;
    			
    			hidden2 = parseFloat(document.average.hidden2.value);
    			hidden3 = parseFloat(document.average.hidden3.value);
    			hidden4 = parseFloat(document.average.hidden4.value);
    			hidden5 = parseFloat(document.average.hidden5.value);
    			hidden6 = parseFloat(document.average.hidden6.value);
    			
    			calcResult = performCalc(hidden2,hiddden3,hidden4,hidden5,hidden6);
    		
    		document.average.averageResult.value=calcResult;
    			
    		}
    		
    			function performCalc(hidden2,hidden3,hidden4,hidden5,hidden6) {
    			
    			var item2Total;
    			var item3Total;
    			var item4Total;
    			var item5Total;
    			var item6Total;
    			var total;
    		
    			item2Total = hidden2 * document.average.num2.value;
    			item3Total = hidden3 * document.average.num3.value;
    			item4Total = hidden4 * document.average.num4.value;
    			item5Total = hidden5 * document.average.num5.value;
    			item6Total = hidden6 * document.average.num6.value;
    		
    		total = (item2Total + item3Total + item4Total + item5Total + item6Total);
    		
    		return total;
    		
    		}
    
    /* ]]> */
    </script>
    <style type="text/css">
    <!--
    .yourPrice {
    	font-weight: bold;
    	color: #F00;
    }
    .product {
    	border-bottom-width: 1px;
    	border-bottom-style: dashed;
    	border-bottom-color: #069;
    }
    .description {
    	font-size: 20px;
    	font-weight: bold;
    }
    -->
    </style>
    </head>
    
    <body>
    <h1>Shopping Cart</h1>
    <p>
    <?php 
    echo "Welcome ". $_SESSION['First_Name']. " ".
    $_SESSION['Last_Name'];
    
    echo "Your Discount is  ". ((1 - $_SESSION['Multiplier']) * 100)."%";
    
    ?>
    </p><form name="average" action="" method="get">
    <?php do { ?>
      <div id="ProductDiv" class="product"><span class="description"><?php echo $row_Recordset1['DESCRIPTION']; ?></span><br />
      <?php echo "Regular Price: $". $row_Recordset1['PRICE'] ?><br /> 
       <img src="images/<?php echo $row_Recordset1['IMAGE']; ?>" width="125" height="125" /><br /><span class="yourPrice"><?php echo "Your Price: $". $row_Recordset1['PRICE'] * $_SESSION['Multiplier'] ; ?></span>
       <br />Quantity <input name="num<?php echo $row_Recordset1['ID']; ?>" type="text"
    onchange="calcTotal()" value="0" size="3" maxlength="3" /><input type="hidden" name="hidden<?php echo $row_Recordset1['ID']; ?>" value="<?php echo $row_Recordset1['PRICE'] * $_SESSION['Multiplier'] ; ?>"/></div>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
      
    </p>
      <strong>Total </strong>
      <input name="averageResult" type="text" style="border: none; " size="6" maxlength="6" /></div>
    </form>
    </body>
    </html>
    <?php
    mysql_free_result($Recordset1);
    ?>

  2. #2
    Join Date
    Oct 2010
    Posts
    4
    Hmm, everything looks fine to me. You could try using getElementById such as:

    document.getElementById("averageResult").value = calcResult;

    instead and add a id to the form field.

Similar Threads

  1. form field to databse help.
    By greenelephant in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 03 Aug 2009, 04:41 PM
  2. Javascript beginner - creating hidden form values from variables
    By javascript_beg in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 29 Jul 2009, 07:25 AM
  3. syntax for declaring a variable as a value in a hidden form field
    By eskimo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 17 Jul 2009, 12:29 AM

Posting Permissions

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