PDA

View Full Version : Calculating a total from hidden form field



kevmatic
07 Oct 2010, 09:05 AM
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!


<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);
?>

Rammus
08 Oct 2010, 10:22 PM
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.