Back to Order Form | Example 1
The code used to generate the order form for example 1:
<?php
$PRODUCTS = array(
// product abbreviation, product name, unit price
// follow valid name/ID rules for product abbreviation
array('choc_cake', 'Chocolate Cake', 15),
array('carrot_cake', 'Carrot Cake', 12),
array('cheese_cake', 'Cheese Cake', 20),
array('banana_bread', 'Banana Bread', 14)
);
function getOrderForm() {
global $PRODUCTS;
$tbl = new HTML_Table(null, 'display', 0, 0, 0);
$frm = new HTML_Form();
// header row
$tbl->addRow();
$tbl->addCell('Product', 'first', 'header');
$tbl->addCell('Price', null, 'header');
$tbl->addCell('Quantity', null, 'header');
$tbl->addCell('Totals', null, 'header');
// display product info/form elements
foreach($PRODUCTS as $product) {
list($abbr, $name, $price) = $product;
$qty_el = $frm->addInput('text', $abbr . '_qty', 0,
array('size'=>4, 'class'=>'cur',
'onchange'=>'getProductTotal(this)',
'onclick'=>'checkValue(this)', 'onblur'=>'reCheckValue(this)' )
);
$tot_el = $frm->addInput('text', $abbr . '_tot', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') );
$price_el = $frm->addInput('hidden', $abbr . '_price', $price);
$tbl->addRow();
$tbl->addCell($name);
$tbl->addCell('$' . number_format($price, 2) . $price_el, 'cur' );
$tbl->addCell( $qty_el, 'qty');
$tbl->addCell( $tot_el );
}
// total row
$tbl->addRow();
$tbl->addCell();
$tbl->addCell();
$tbl->addCell( 'Total: ', 'total');
$tbl->addCell( $frm->addInput('text', 'total', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
// submit button
$tbl->addRow();
$tbl->addCell();
$tbl->addCell( $frm->addInput('submit', 'submit', 'Submit') );
$tbl->addCell();
$tbl->addCell();
$frmStr = $frm->startForm('ex1_result.php', 'post', null,
array('onsubmit'=>'return finalCheck(this)') ) .
$tbl->display() . $frm->endForm();
return $frmStr;
}
// for js
function getProductAbbrs() {
global $PRODUCTS;
foreach ( $PRODUCTS as $product ) {
$ar[] = $product[0];
}
return $ar;
}
?>
To output product abbreviations for use by JavaScript:
<script type="text/javascript">
// <![CDATA[
var PRODUCT_ABBRS = <?php echo json_encode( getProductAbbrs() ) ?>;
// ]]>
</script>
<?php
function getOrderInfo() {
global $PRODUCTS;;
$str = ''; $total = 0;
while ( list($key, $val) = each($_POST) ) {
// Check for valid quantity entries > 0
if ( ( strpos($key, '_qty') !== false ) && is_int( (int)$val) && $val > 0 ) {
$pt = strrpos($key, '_qty');
$name_pt = substr( $key, 0, $pt); // product abbr
foreach($PRODUCTS as $product) {
list($prod_abbr, $prod_name, $prod_price) = $product;
if ($prod_abbr == $name_pt) {
$sub_tot = $val * $prod_price;
// build string to display order info
$str .= "<p>$val $prod_name at $" . number_format($prod_price, 2) .
' each for $' . number_format($sub_tot, 2) . '</p>';
$total += $sub_tot;
}
}
}
}
if ( $str === '' ) {
$str = '<p>You didn\'t order anything.</p>';
} else {
$str = '<h2>Your Order:</h2>' . $str . '<p>Total: $' . number_format($total, 2) . '</p>' . getPayPalBtn($total);
}
return $str;
}
?>
Download the order form examples.
Back to Order Form | Top