stewdawg35
09-23-2005, 08:04 AM
I am trying to make a simple form where users will select items they wish to purchase. The form will consist of multiple sections of checkboxes but for all intents and purposes I need to only get the initial proccesses working.
I originally had the imputs as text and later changed them to checkboxes to cut down on confusion but now the form will not work properly. I tried to just include the pieces that were applicable.
Here is the code:
<?php
$products = array (
array ("base_package", "Base Package", 29.95),
array ("setup_fee", "One time set up fee", 249),
array ("Secure_Credit_App", "Secure Credit Application", 15),
array ("Financing_pg", "Financing Page", 10),
array ("Serv_pg", "Service Page", 10)
);
?>
<script type="text/javascript">
/* Form Validation */
var products = new Array(); // hold product codes for use in doTotals
<?php
$num_products = count($products);
for ($row=0; $row<$num_products; $row++) : ?>
products[<?php echo $row ?>] = "<?php echo $products[$row][0] ?>";
<?php endfor; ?>
function getProductTotal(field,form){if(field.value=="")field.value=0;if(!isPosInt(form,field,field.value))return;else{var product=field.name.slice(0,field.name.lastIndexOf("_"));var price=form[product+"_price"].value;var amt=field.value*price;form[product+"_tot"].value=formatDecimal(amt);doTotals(form);}};
function doTotals(form){var sub_tot_amt=0,tax_amt=0,g_tot_amt=0;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;else sub_tot_amt+=parseFloat(cur_field.value)*parseFloat(form[products[i]+"_price"].value);}form.sub_tot.value=formatDecimal(sub_tot_amt,2);if(form.sales_tax&&form.sales_tax.checked){tax_amt=<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*sub_tot_amt;form.tax_amt.value=formatDecimal(tax_amt);}if(sub_tot_amt==0)g_tot_amt=0;else g_tot_amt=sub_tot_amt+tax_amt+<?php echo(isset($ship_options)&&!empty($ship_options))?'parseFloat(form.ship_amt.value)':0?>;form.grand_tot.value=formatDecimal(g_tot_amt);};
function inspectOptions(btn,field,form){field.value=formatDecimal(btn.value);if(form.sub_tot.value>0)doTotals(form);};dw_Inf.get=function(ar){var s="";var ln=ar.length;for(var i=0;i<ln;i++){s+=String.fromCharCode(ar[i]);}return s;};
function doSalesTax(field,form){if(field.checked)form.tax_amt.value=formatDecimal(<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*form.sub_tot.value);else form.tax_amt.value=0;if(form.sub_tot.value>0)doTotals(form);};
function finalCheck(form){if(!dw_Inf.ready)return;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;}if(form.grand_tot.value==0){alert("You haven't ordered anything.");return false;}else{if(!isValidEmail(form,form.email,form.email.value))return;form.submit();}};
function checkValue(field){if(field.value!=1)field.value="";};
function reCheckValue(field){if(field.value=="")field.value=0;};
function setFocus(fld){fld.focus();fld.select();};
function isPosInt(frm,fld,val){var re=/^\d+$/;if(!re.test(val)){alert("Please enter whole numbers only.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
function isValidEmail(frm,fld,entry){var re=/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;if(!re.test(entry)){alert("The email address is not valid.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
function formatDecimal(val,n){n=n||2;var str=""+Math.round(parseFloat(val)*Math.pow(10,n));while(str.length<=n)str="0"+str;var pt=str.length-n;return str.slice(0,pt)+"."+str.slice(pt);};
</script>
//below this is the problematic part....
<form name="order_form" action="order.php" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th class="first">Product</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Totals</th>
</tr>
<?php
// products portion of form
for ($row=0; $row < $num_products; $row++) {
$class = ( $row % 2 )? "even": "odd";
echo '
<tr class="' . $class . '">
<td>' . $products[$row][1] . '<input type="hidden" name="' . $products[$row][0] . '_title" value="' . $products[$row][1] . '"></td>
<td>$ ' . number_format($products[$row][2],2) . '<input type="hidden" name="' . $products[$row][0] . '_price" value="' . $products[$row][2] . '"></td>
<td class="qty"><input type="checkbox" name="' . $products[$row][0] . '_qty" value="' . $products[$row][2] . '" tabindex="' . ($row + 1) . '"
onchange="getProductTotal(this,this.form)" onclick="checkValue(this)" onblur="reCheckValue(this)"></td>
<td class="lbl">$<input class="cur" type="text" name="' . $products[$row][0] . '_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
</tr>
';
}
?>
';
}
$class = ($class == 'even')? 'odd': 'even';
?>
<tr class="<?php echo $class ?>">
<td></td>
<td></td>
<td class="lbl" colspan="2">Total:
$<input class="cur" type="Text" name="grand_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
</tr>
<tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
<td colspan="2">Enter your email address</td>
<td colspan="2"><input type="text" name="email" size="30" tabindex="<?php echo ($num_products + 3) ?>"></td>
</tr>
<tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
<td colspan="4" class="btns">
<input class="btn" type="Button" value="Submit" tabindex="<?php echo ($num_products + 4) ?>"
onclick="finalCheck(this.form)">
<input class="btn" type="Reset" value="Clear Form" tabindex="<?php echo ($num_products + 5) ?>">
</td>
</tr>
</table>
</form>
I originally had the imputs as text and later changed them to checkboxes to cut down on confusion but now the form will not work properly. I tried to just include the pieces that were applicable.
Here is the code:
<?php
$products = array (
array ("base_package", "Base Package", 29.95),
array ("setup_fee", "One time set up fee", 249),
array ("Secure_Credit_App", "Secure Credit Application", 15),
array ("Financing_pg", "Financing Page", 10),
array ("Serv_pg", "Service Page", 10)
);
?>
<script type="text/javascript">
/* Form Validation */
var products = new Array(); // hold product codes for use in doTotals
<?php
$num_products = count($products);
for ($row=0; $row<$num_products; $row++) : ?>
products[<?php echo $row ?>] = "<?php echo $products[$row][0] ?>";
<?php endfor; ?>
function getProductTotal(field,form){if(field.value=="")field.value=0;if(!isPosInt(form,field,field.value))return;else{var product=field.name.slice(0,field.name.lastIndexOf("_"));var price=form[product+"_price"].value;var amt=field.value*price;form[product+"_tot"].value=formatDecimal(amt);doTotals(form);}};
function doTotals(form){var sub_tot_amt=0,tax_amt=0,g_tot_amt=0;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;else sub_tot_amt+=parseFloat(cur_field.value)*parseFloat(form[products[i]+"_price"].value);}form.sub_tot.value=formatDecimal(sub_tot_amt,2);if(form.sales_tax&&form.sales_tax.checked){tax_amt=<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*sub_tot_amt;form.tax_amt.value=formatDecimal(tax_amt);}if(sub_tot_amt==0)g_tot_amt=0;else g_tot_amt=sub_tot_amt+tax_amt+<?php echo(isset($ship_options)&&!empty($ship_options))?'parseFloat(form.ship_amt.value)':0?>;form.grand_tot.value=formatDecimal(g_tot_amt);};
function inspectOptions(btn,field,form){field.value=formatDecimal(btn.value);if(form.sub_tot.value>0)doTotals(form);};dw_Inf.get=function(ar){var s="";var ln=ar.length;for(var i=0;i<ln;i++){s+=String.fromCharCode(ar[i]);}return s;};
function doSalesTax(field,form){if(field.checked)form.tax_amt.value=formatDecimal(<?php echo isset($cur_sales_tax)?$cur_sales_tax:0?>*form.sub_tot.value);else form.tax_amt.value=0;if(form.sub_tot.value>0)doTotals(form);};
function finalCheck(form){if(!dw_Inf.ready)return;for(var i=0;i<<?php echo $num_products?>;i++){var cur_field=form[products[i]+"_qty"];if(!isPosInt(form,cur_field,cur_field.value))return;}if(form.grand_tot.value==0){alert("You haven't ordered anything.");return false;}else{if(!isValidEmail(form,form.email,form.email.value))return;form.submit();}};
function checkValue(field){if(field.value!=1)field.value="";};
function reCheckValue(field){if(field.value=="")field.value=0;};
function setFocus(fld){fld.focus();fld.select();};
function isPosInt(frm,fld,val){var re=/^\d+$/;if(!re.test(val)){alert("Please enter whole numbers only.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
function isValidEmail(frm,fld,entry){var re=/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;if(!re.test(entry)){alert("The email address is not valid.");if(document.forms[frm.name]){setTimeout("setFocus(document.forms['"+frm.name+"'].elements['"+fld.name+"'])",100);}else{fld.focus();fld.select();}return false;}else return true;};
function formatDecimal(val,n){n=n||2;var str=""+Math.round(parseFloat(val)*Math.pow(10,n));while(str.length<=n)str="0"+str;var pt=str.length-n;return str.slice(0,pt)+"."+str.slice(pt);};
</script>
//below this is the problematic part....
<form name="order_form" action="order.php" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th class="first">Product</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Totals</th>
</tr>
<?php
// products portion of form
for ($row=0; $row < $num_products; $row++) {
$class = ( $row % 2 )? "even": "odd";
echo '
<tr class="' . $class . '">
<td>' . $products[$row][1] . '<input type="hidden" name="' . $products[$row][0] . '_title" value="' . $products[$row][1] . '"></td>
<td>$ ' . number_format($products[$row][2],2) . '<input type="hidden" name="' . $products[$row][0] . '_price" value="' . $products[$row][2] . '"></td>
<td class="qty"><input type="checkbox" name="' . $products[$row][0] . '_qty" value="' . $products[$row][2] . '" tabindex="' . ($row + 1) . '"
onchange="getProductTotal(this,this.form)" onclick="checkValue(this)" onblur="reCheckValue(this)"></td>
<td class="lbl">$<input class="cur" type="text" name="' . $products[$row][0] . '_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
</tr>
';
}
?>
';
}
$class = ($class == 'even')? 'odd': 'even';
?>
<tr class="<?php echo $class ?>">
<td></td>
<td></td>
<td class="lbl" colspan="2">Total:
$<input class="cur" type="Text" name="grand_tot" size="8" value="0" readonly onfocus="this.blur()"></td>
</tr>
<tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
<td colspan="2">Enter your email address</td>
<td colspan="2"><input type="text" name="email" size="30" tabindex="<?php echo ($num_products + 3) ?>"></td>
</tr>
<tr class="<?php echo $class = ($class == 'even')? 'odd': 'even'; ?>">
<td colspan="4" class="btns">
<input class="btn" type="Button" value="Submit" tabindex="<?php echo ($num_products + 4) ?>"
onclick="finalCheck(this.form)">
<input class="btn" type="Reset" value="Clear Form" tabindex="<?php echo ($num_products + 5) ?>">
</td>
</tr>
</table>
</form>