Jump to content

Problem getting function to update Total box


The.Pr0fess0r

Recommended Posts

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:

<?PHP

session_start();
if(!session_is_registered(myusername)){
header("location: login.php");
}

foreach ($_REQUEST as $key => $value) {
$$key = $value;
}

include ("functions.inc");
include ("connection.inc");
db_connect ()
or die ("Cannot connect to server");

$query = "SELECT * FROM fulfillment_users WHERE username = '" . $_SESSION['useremail'] . "'";
$result = @mysql_query ($query) or die ("$query");

while ($row = mysql_fetch_array ($result)) {
$usertype = $row['type'];
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<SCRIPT language=JavaScript src="popup.js"></SCRIPT>
<script type="text/javascript" src="includes/prototype.js"></script>
<script type="text/javascript" src="includes/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="includes/lightbox.js"></script>
<script type="text/javascript" src="includes/autocomplete.js"></script>
<link rel="stylesheet" href="lightbox.css" type="text/css" media="screen" />

<link rel="stylesheet" type="text/css" href="styles/autocomplete.css" />
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
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 fulfillment_items WHERE itemType LIKE '%" . $usertype . "%' ORDER BY itemOrder";
}else{
$query = "SELECT * FROM fulfillment_items ORDER BY itemOrder";
}
$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)
}
}
// End -->
</SCRIPT>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1;no-cache">
<meta name="generator" content="Adobe Golive">
<title>Biewer Marketing Materials</title>
<style type="text/css" media="screen"><!--
.class { font-size: 18px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold }
.title { font-size: 16px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold }
td { font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif }
--></style>
</head>
<body onload="updateTotal();">
<div align="center">
<table width="700" border="0" cellspacing="0" cellpadding="0">
  <tr>
  <td>
   <table width="685" border="0" cellspacing="0" cellpadding="0">
   <tr>
    <td nowrap width="50%"><a href="index.php"><img src="images/logo.gif" alt="" border="0"></a></td>
    <td align="right" valign="bottom" nowrap width="50%"><span class="class">Biewer Marketing Materials</span></td>
   </tr>
   </table>
  </td>
  </tr>
  <tr>
  <td>
   <hr>
  </td>
  </tr>
  <tr>
  <td width="685" align="center">

  <?PHP

   if($n != "") {
   if($n == "added") $notify = "Order Has Been Added";
   if($n == "updated") $notify = "Order Has Been Updated";
  ?>
   <table cellspacing="0" cellpadding="5" width="100%" style="background: #ffcccc; border-style: solid; border-width: 2px; border-color: #ff0000">
   <tr><td width="100%" align="center" style="color: #000000"><?=$notify;?></td></tr>
   </table><br>
  <?PHP
   }
  ?>
   <form action="index.php" method="post" name="form">
   <table cellpadding="0" cellspacing="0">
    <tr><td colspan="10"><font size="+1">Address</font>  <a href="index.php">[clear]</a></td></tr>
    <tr><td width="20"></td><td nowrap>Existing Address:</td><td></td><td colspan="10">Type name and select from dropdown to auto-populate address<br>

<?PHP

    if($addressbook > 0) {

    $query = "SELECT * FROM fulfillment_addressbook WHERE addrId = $addressbook";
    $result = @mysql_query ($query) or die ("$query");

    while ($row = mysql_fetch_array ($result)) {
     $name = $row['addrName'];
     $store = $row['addrStore'];
     $address = $row['addrAddress'];
     $city = $row['addrCity'];
     $state = $row['addrState'];
     $zip = $row['addrZip'];
     $phone = $row['addrPhone'];
     $fax = $row['addrFax'];
     $email = $row['addrEmail'];
    }
    }

    $item_qty = array();

    $query = "SELECT * FROM fulfillment_order_detail";
    $result = @mysql_query ($query) or die ("$query");

    while ($row = mysql_fetch_array ($result)) {
    $item_qty[$row['itemNumb']] -= $row['itemQty'];
//     $item_qty[$row['itemNumb']] = 10;
    }

    $query = "SELECT * FROM fulfillment_adjustment";
    $result = @mysql_query ($query) or die ("$query");

    while ($row = mysql_fetch_array ($result)) {
    $item_qty[$row['itemNumb']] += $row['adjQty'];
//     $item_qty[$row['itemNumb']] = 10;
    }

    if($orderId > 0) {

    $query = "SELECT * FROM fulfillment_order_header WHERE orderNumb = $orderId";
    $result = @mysql_query ($query) or die ("$query");

    while ($row = mysql_fetch_array ($result)) {
     $name = $row['orderName'];
     $store = $row['orderStore'];
     $address = $row['orderAddr'];
     $city = $row['orderCity'];
     $state = $row['orderState'];
     $zip = $row['orderZip'];
     $phone = $row['orderPhone'];
     $fax = $row['orderFax'];
     $email = $row['orderEmail'];
     $po = $row['orderPO'];
     $cc_number = $row['orderCCNumber'];
     $cc_expire = $row['orderCCExpire'];
     $shipping = $row['orderShipping'];
     $shipping_cost = number_format($row['orderShippingAmount'],2);
     $other_cost = number_format($row['orderOtherAmount'],2);
     $total = number_format($row['orderTotalAmount'],2);
    }

    $query = "SELECT * FROM fulfillment_order_detail WHERE orderNumb = $orderId";
    $result = @mysql_query ($query) or die ("$query");

    $order_qty = array();
    $order_price = array();
    $item_subtotal = array();
    while ($row = mysql_fetch_array ($result)) {
     $order_qty[$row['itemNumb']] = $row['itemQty'];
     $item_qty[$row['itemNumb']] += $row['itemQty'];
     $item_subtotal[$row['itemNumb']] = number_format(($row['itemQty'] * $row['itemPrice']),2);
//     $order_price[$row['itemNumb']] = $row['itemPrice'];
    }

    }

?>

<input type="text" name="addressbook1" value="<?=$name;?>" style="width: 300px">
<script type="text/javascript">

new CAPXOUS.AutoComplete("addressbook1", function() {
  return "includes/autocomplete1.php?typing=" + this.text.value;
});

</script>

    </td></tr></form>
    <tr><td> </td></tr>
    <form action="order.php" method="post" name="items">
    <input type="hidden" name="useremail" value="<?=$_SESSION["useremail"];?>">
    <input type="hidden" name="usertype" value="<?=$usertype;?>">
<?PHP
    if($addressbook > 0) {
?>
    <input type="hidden" name="addressbook" value="<?=$addressbook;?>">
<?PHP
    }
?>
<?PHP
    if($orderId > 0) {
?>
    <input type="hidden" name="orderId" value="<?=$orderId;?>">
<?PHP
    }
?>
    <tr><td width="20"></td><td>Name:</td><td width="10"></td><td colspan="10"><input type="text" name="name" size="30" value="<?=$name?>"></td></tr>
    <tr><td width="20"></td><td>Store Name:</td><td width="10"></td><td colspan="10"><input type="text" name="store" size="30" value="<?=$store?>"></td></tr>
    <tr><td width="20"></td><td>Address:</td><td width="10"></td><td colspan="10"><input type="text" name="address" size="30" value="<?=$address?>"></td></tr>
    <tr><td width="20"></td><td>City:</td><td width="10"></td><td><input type="text" name="city" size="16" value="<?=$city?>"></td>
    <td width="5"></td><td>State:</td><td width="10"></td><td>
     <select name="state">
     <? form_state($state); ?>
     </select>
    </td>
    <td width="20"></td><td>Zip:</td><td width="10"></td><td><input type="text" name="zip" size="10" value="<?=$zip?>"></td></tr>
    <tr>
    <td width="20"></td><td>Phone:</td><td width="10"></td><td><input type="text" name="phone" size="16" value="<?=$phone?>"></td>
    <td width="5"></td><td>Fax:</td><td width="10"></td><td><input type="text" name="fax" size="16" value="<?=$fax?>"></td>
    </tr>
    <tr><td width="20"></td><td>Email:</td><td width="10"></td><td colspan="10"><input type="text" name="email" size="30" value="<?=$email?>"></td></tr>
    <tr><td> </td></tr>
    <tr><td width="20"></td><td>PO Number:</td><td width="10"></td><td colspan="10"><input type="text" name="po" size="30" value="<?=$po?>"></td></tr>
    <tr>
    <td width="20"></td><td nowrap>Shipping Method:</td><td width="10"></td>
    <td>
     <select name="shipping">
     <option value="ground" <? if($shipping == "ground") echo "selected"; ?>>Ground
     <option value="secondday" <? if($shipping == "secondday") echo "selected"; ?>>Second Day
     <option value="nextdayafternoon" <? if($shipping == "nextdayafternoon") echo "selected"; ?>>Next Day (5:00pm)
     <option value="nextafternoon" <? if($shipping == "nextafternoon") echo "selected"; ?>>Next Afternoon
     <option value="nextdaymorning" <? if($shipping == "nextdaymorning") echo "selected"; ?>>Next Day (10:30am)
     <option value="satdelivery" <? if($shipping == "satdelivery") echo "selected"; ?>>Saturday Delivery
     </select>
    </td>
    </tr>
   <table>
  </td>
  </tr>
  <tr><td> </td><tr>
  <tr>
  <td width="685" align="center">
   <table cellspacing="0" cellpadding="0">
   <tr>
    <td align="center" valign="top"><font size="+1">Item#</font></td><td></td>
    <td align="center" valign="top"><font size="+1">Description</font><br>(Click product description for image)</td><td></td>
    <td align="center" valign="top"><font size="+1">Price</font></td><td></td>
    <td align="center" valign="top"><font size="+1">Stock</font></td><td></td>
    <td align="center" valign="top"><font size="+1">Qty</font></td><td></td>
    <td align="center" valign="top"><font size="+1">Total</font></td><td></td>
   </tr>
   <tr><td> </td></tr>
<?PHP

   if($usertype != '') {
    $query = "SELECT * FROM fulfillment_items WHERE itemType LIKE '%" . $usertype . "%' ORDER BY itemOrder";
   }else{
    $query = "SELECT * FROM fulfillment_items ORDER BY itemOrder";
   }
   $result = @mysql_query ($query) or die ("$query");

   $i=0;
   while ($row = mysql_fetch_array ($result)) {

    $itemDesc = $row['itemDesc'];
    if($row['itemBrand'] != '') $itemDesc .= ' (Logo: ' . $row['itemBrand'] . ')';
    if($row['itemColor'] != '') $itemDesc .= ' (' . $row['itemColor'] . ')';
    if($row['itemSize'] != '') $itemDesc .= ' (' . $row['itemSize'] . ')';
    if($row['itemLogoColor'] != '') $itemDesc .= ' (' . $row['itemLogoColor'] . ')';

    if($row['itemHeader'] != '') {
?>
   <tr><td> </td></tr>
   <tr><td></td><td></td><td align="center"><b><?=str_replace("\n","<br>",$row['itemHeader']);?></b></td></tr>
   <tr><td> </td></tr>
<?PHP
    }

    if(fmod($i++,2)==0) { $color = "#FFFFFF"; } else { $color = "#FFFFFF"; }
    if(!file_exists("images/" . $row['itemImage'])) $row['itemImage'] = "coming_soon.jpg";
?>
   <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>
   <input type="submit" value="Submit">
   </form>
  </td>
  </tr>
  <tr>
  <td>
   <hr>
  </td>
  </tr>
  <tr>
  <td width="685" align="center">
   <form action="index.php" method="post" name="form">
   <table cellpadding="0" cellspacing="0">
    <tr><td colspan="10"><font size="+1">Open Orders</font><br><br></td></tr>
<?PHP

   $query = "SELECT * FROM fulfillment_order_header WHERE orderStatus = 1 ORDER BY orderNumb DESC";
   $result = @mysql_query ($query) or die ("$query");

   if(mysql_num_rows($result) == 0) echo '<tr><td colspan="10" align="center">No Open Orders</td></tr>';

   $i=0;
   while ($row = mysql_fetch_array ($result)) {
    if(fmod($i++,2)==0) { $color = "#DCDCDC"; } else { $color = "#FFFFFF"; }
?>
   <tr bgcolor="<?=$color;?>">
    <td width="40" valign="top" align="right"><?=$row['orderNumb'];?></td><td width="10"></td>
    <td width="200" valign="top"><a href="index.php?orderId=<?=$row['orderNumb'];?>"><?=$row['orderName'];?> <?=$row['orderStore'];?></a></td><td width="10"></td>
    <td width="200" valign="top"><?=$row['orderCity'];?>, <?=$row['orderState'];?> <?=$row['orderZip'];?></td><td width="10"></td>
    <td width="50" valign="top" align="right">$<?=number_format($row['orderTotalAmount'],2);?></td><td width="10"></td>
    <td width="155" valign="top" align="right"><?=$row['orderDate'];?></td>
    <td><img src="images/blank.gif" height="24" width="1"></td>
   </tr>
<?PHP
   }
?>

</table><br><br>
<a href="logout.php">[logout]</a>
</div>
</body>
</html> 

 

It's the function updateTotal() that I am having problems with.

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

If you want help with a JavaScript problem you are much more likely to get a response if you post the "JavaScript" and not the PHP code. You have JavaScript code that is dynamically generated based upon database results. Not knowing what is in your database I have no idea what that rendered code would look like.

 

Please post code from the rendered HTML output from the script. If the error is in the core javascript we should be able to help. If the error has to do with the dynamic code we should be abble to identify that and then give next steps to further debug.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.