Forum Moderators: open
I have the form displaying properly and the subtotals are working, but I cannot for the life of me get the Total box at the bottom of the form to work. It is just blank no matter what.
Basically my form has 6 columns. The form lists all items available to the salesman. The last two columns are qty and total (for that item). I am able to input a qty and the total for that item will appear properly.
At the bottom of my form I have a total box that I want to populate with the total for the entire form and I cannot get that to populate for the life of me.
My code is as follows:
<snipped code dump per TOS, see shorter relevant code snippets below!>
It's the function updateTotal() that I am having problems with.
Any help would be greatly appreciated.
[edited by: whoisgregg at 7:15 pm (utc) on Sep. 2, 2009]
[edit reason] Whoops, no code dumps. See TOS [webmasterworld.com] :) [/edit]
Being the Javascript forum, this will eliminate PHP out of the equation and we can see the forest from the trees at a glance.
function formHandler(form){
var URL = 'index.php?addressbook=' + document.form.addressbook.options[document.form.addressbook.selectedIndex].value;
window.location.href = URL;
}function updateTotal() {
var totalval = 0
<?PHP
if($usertype != '') {
$query = "SELECT * FROM items WHERE Type LIKE '%" . $usertype . "%' ORDER BY Order";
}else{
$query = "SELECT * FROM items ORDER BY Order";
}
$result = @mysql_query ($query) or die ("$query");
$allitem = '';
while ($row = mysql_fetch_array ($result)) {
echo ' if(document.items.elements[\'b' . $row['itemNumb'] . '\'].value > 0) totalval = totalval + parseFloat(document.items.elements[\'b' . $row['itemNumb'] . '\'].value)' . "\n";
}
?>
document.items.elements['shipping_cost'].value = parseFloat(document.items.elements['shipping_cost'].value * 1).toFixed(2)
document.items.elements['other_cost'].value = parseFloat(document.items.elements['other_cost'].value * 1).toFixed(2)
totalval = totalval + parseFloat(document.items.elements['shipping_cost'].value)
totalval = totalval + parseFloat(document.items.elements['other_cost'].value)
document.items.total.value = totalval.toFixed(2)
}
function updateSubtotal(qty,sub,price) {
var quantity = document.items.elements[qty].value * 1
var subtotal = quantity * price
if(quantity > 0) {
document.items.elements[sub].value = subtotal.toFixed(2)
} else {
document.items.elements[sub].value = ''
}
updateTotal()
}
function checkQty(qty,item,sub,price) {
if(document.items.elements[item].value > qty) {
var newqty
var string = 'Maximum order quantity is: ' + qty
alert(string)
if(qty > 0) {
newqty = qty
} else {
newqty = ''
}
document.items.elements[item].value = newqty
updateSubtotal(item,sub,price)
}
}
</SCRIPT>
It's the updateTotal function that is not working properly.
If it helps here is the html/php code where I am trying to call the functions:
<tr bgcolor="<?=$color;?>">
<td width="100" valign="top" nowrap><?=$row['itemNumb'];?></td><td width="10"></td>
<td width="380" valign="top"><a href="images/<?=$row['itemImage'];?>" title="<?=$row['itemDescLong'];?>" rel="lightbox"><?=$itemDesc;?></a><? if($row['itemNew'] != '' && $row['itemNew'] >= date("Ymd")) echo ' <font color="red">***NEW***</font>'; ?><br><?=$row['itemDetail'];?></td><td width="10"></td>
<td align="right" valign="top">$<?=number_format($row['itemPrice'],2);?></td><td width="10"></td>
<td align="right" valign="top"><?=$item_qty[$row['itemNumb']]+0;?></td><td width="10"></td>
<td align="center" valign="top" nowrap>
<?PHP
if($item_qty[$row['itemNumb']] <= 0) {
echo 'Out';
} else {
// $max_order = min($item_qty[$row['itemNumb']],10);
$max_order = $item_qty[$row['itemNumb']];
if($row['itemNumb'] == '65000') $max_order = 2;
echo '<input type="text" name="a' . trim($row['itemNumb']) . '" value="' . $order_qty[$row['itemNumb']] . '" size="3" STYLE="text-align:right" onkeyup="updateSubtotal(\'a' . trim($row['itemNumb']) . '\',\'b' . trim($row['itemNumb']) . '\',' . $row['itemPrice'] . '); checkQty(' . max(0,$max_order) . ',\'a' . trim($row['itemNumb']) . '\',\'b' . trim($row['itemNumb']) . '\',' . $row['itemPrice'] . ')" >';
}
?>
</td>
<td width="10"></td><td valign="top" align="right"><input type="text" size="7" name="b<?=trim($row['itemNumb']);?>" value="<?=$item_subtotal[$row['itemNumb']];?>" STYLE="text-align:right" tabindex="999" readonly></td>
<td><img src="images/blank.gif" height="24" width="1"></td>
</tr>
<?PHP
}
?>
<tr><td colspan="9" align="right">Shipping:</td><td></td><td align="right" colspan="2"><input type="text" size="7" name="shipping_cost" value="<?=$shipping_cost;?>" onblur="updateTotal()" STYLE="text-align:right" tabindex="997">
<tr><td colspan="9" align="right">Other:</td><td></td><td align="right" colspan="2"><input type="text" size="7" name="other_cost" value="<?=$other_cost;?>" onblur="updateTotal()" STYLE="text-align:right" tabindex="998"></td>
<tr><td colspan="9" align="right">Total:</td><td></td><td align="right" colspan="2"><input type="text" size="7" name="total" value="<?=$total;?>" STYLE="text-align:right" tabindex="999" readonly></td><td><img src="images/blank.gif" height="24" width="1"></td></tr>
</table><br>
Hopefully that is a little bit easier to read and will get a couple of responses.
Thanks for any help.