I will try to keep this simple. Here is the script for an order form I am doing for a client. She is very specific about what she wants. The loop pulls products from a file, and displays them in a table with alternating colors. The user selects a quantity for ea. product, and the total price shows up in an input box. The grand total of all orders should show up at the bottom of the page.
Questions: First, the grand total is not working, and I don't know why. I think it has to do with my javascript array. Second, how do I mail all the product info to the client when the variables have been made up on the fly?
Thanks!!
<form method="post" action="<? print("$PHP_SELF"); ?>" name="form">
<script language="Javascript">
<!--
function createArray(size) {
for (var i=0; i < size; i++) {
this = null }
return this
}
function Cost(name) {
this.amount = name;
}
function SetLengths() {
var k=1;
while(cost[k] != null)
k++
cost.length = k;
}
var cost = new createArray(1);
//-->
</script>
<?
/ alternate bgcolor in a table /
function useColor(){
/ remember the last color we used /
static $ColorValue;
/* choose the next color */
if($ColorValue == "#990000"){
$ColorValue = "#909090";
}
else{
$ColorValue = "#990000";
}
return($ColorValue);
}
/* create list of product categories */
$categories = file("titles.dat");
for ($c=0, $function=0; $c<count($categories), $function<count($categories); $c++, $function++){
$categories[$c] = stripslashes($categories[$c]);
$text = explode("|", $categories[$c]);
$subject = $text[0];
$image = $text[1];
$file = $text[2];
$url = $text[3];
$target = $text[4];
/* print table header */
print "<center><a name=\"$target\" href=\"$url\"><img src=\"$image\" width=\"411\" height=\"87\" border=\"0\"></a>\n";
print "<table width=\"600\" cellpadding=\"3\" cellspacing=\"3\" border=\"0\">\n";
/* print taste of texas content */
$products = file("$file");
for ($i=0, $number=0; $i<count($products), $number<count($products); $i++, $number++){
$products[$i] = stripslashes($products[$i]);
$info = explode("|", $products[$i]);
$title = $info[0];
$productname = $info[1];
$price = $info[2];
/* get color for this row */
$RowColor = useColor();
/* print out HTML for row and set bgcolor */
print "<tr>\n";
print "<td bgcolor=\"$RowColor\" width=\"400\"><font face=\"arial\" color=\"#000000\" size=\"-1\"><b>$title</b><br>\n";
print "$productname -- \$$price\n";
print "<input type=\"hidden\" name=\"product\" value=\"$productname\">\n";
print "<input type=\"hidden\" name=\"price$function$number\" value=\"$price\"></font></td>\n";
print "<td bgcolor=\"$RowColor\" width=\"100\"><font face=\"arial\" color=\"#000000\" size=\"-1\"><b>Quantity:</b>\n";
print "<select name=\"quantity$function$number\" onChange=\"compute$function$number(this.form); compute_total(this.form);\">\n";
for($count=0; $count < 10; $count++)
{
print "<option value=\"$count\"";
if ($product_quantity == '$count'){ print(" selected"); }
print ">$count\n";
}
print "</select>\n";
print "</font></td>\n";
print "<td align=\"center\" bgcolor=\"$RowColor\" width=\"125\"><font face=\"arial\" color=\"#000000\" size=\"-1\"><b>\n";
print "<script language=\"JavaScript\">\n";
print "<!--// \n";
print "function compute$function$number(form) {\n";
print "form.cost$function$number.value = eval((form.price$function$number.value)*(form.quantity$function$number.value));\n";
print "}\n";
print "//-->\n";
print "</script>\n";
print "Price: <input type=\"text\" name=\"cost$function$number\" size=\"5\" onFocus=\"blur()\">";
print "<script language=\"JavaScript\">\n";
print "<!--// \n";
print "cost[$function$number] = new Cost(form.cost$function$number.value);";
print "//-->\n";
print "</script>\n";
print "</font></td>\n";
print "</tr>\n";
}
/* print table footer */
print "<tr>\n";
print "</table></center>\n";
print "<p align=\"center\">\n";
print "<font face=\"arial\" size=\"-1\"><a href=\"#order\">Place Order</a></font>\n";
print "<p align=\"center\">\n";
}
print "<script language=\"JavaScript\">\n";
print "<!--// \n";
print "function compute_total(form) { \n";
print "for (i=1; i<cost.length; i++){
form.total_cost.value = eval((0) += ('document.form.cost' + i + '.value'));}\n";
print "}\n";
print "//-->\n";
print "</script>\n";
print "<table width=\"600\" cellpadding=\"3\" cellspacing=\"3\" border=\"0\">\n";
print "<tr>\n";
print "<td align=\"right\" bgcolor=\"#990000\"><font face=\"arial\" color=\"#000000\" size=\"-1\">\n";
print "<b>SUBTOTAL</b> (does not include shipping):\n";
print "<input type=\"text\" name=\"total_cost\" size=\"5\" onFocus=\"blur()\"> </font></td>\n";
print "</tr>\n";
?>
</form>