Forum Moderators: open

Message Too Old, No Replies

Problem Getting Total box to Populate

         

ThePr0fess0r

4:30 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



Hello, I am in the process of creating a site our salesman can use to place orders for marketing items to give their customers.

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]

rocknbil

5:20 pm on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ThePr0fess0r, the above is a bit much to digest and may not get many responses. Can you run the PHP, view/copy the resulting page source, then post the HTML/JS here, eliminating URL's that would lead to your site (per the TOS)? Try to cut it down a bit.

Being the Javascript forum, this will eliminate PHP out of the equation and we can see the forest from the trees at a glance.

ThePr0fess0r

6:09 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



OK, I ran the page and viewed the source code...but since my db is already populated the output was over 5000 lines of code. So, to make it easier (and what I should have done in the first place) here are just my scripts:


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.

whoisgregg

7:23 pm on Sep 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's the updateTotal function that is not working properly.

Do you receive an error? Does the total quantity or value change at all, like perhaps to NaN or undefined?

Also, what happens if you alert(totalval); right after the php block that writes out the individual line items?