Back to Order Form | Example 2
The code used to generate the order form for example 2:
<?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 getOrderForm2() {
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') ) );
/////////////////////////////////////////////////////////////////////
// additional fields for contact info
$tbl->addRow();
$tbl->addCell('First Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'first_name', '', array('size'=>36 ) )
, null, 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Last Name: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'last_name', '', array('size'=>36) )
, null, 'data', array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Email: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'email', '', array('size'=>36) ), null, 'data',
array('colspan'=>3)
);
$tbl->addRow();
$tbl->addCell('Phone: ', 'label');
$tbl->addCell(
$frm->addInput('text', 'phone', '', array('size'=>36) ), null, 'data',
array('colspan'=>3)
);
//
/////////////////////////////////////////////////////////////////////
// submit button
$tbl->addRow();
$tbl->addCell();
$tbl->addCell( $frm->addInput('submit', 'submit', 'Submit') );
$tbl->addCell();
$tbl->addCell();
$frmStr = $frm->startForm('ex2_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 sendAdminEmail($total, $order) {
$admin_email = 'your_addy@your.com';
$subject = 'Order Information';
$name = stripslashes( strip_tags( $_POST['first_name'] ) ) . ' ' .
stripslashes( strip_tags( $_POST['last_name'] ) );
$email = stripslashes( strip_tags( $_POST['email'] ) );
// check for valid email address
$regex = '/^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$/';
if ( !preg_match($regex, $_POST['email']) ) {
// don't send email
echo '<p>Your email appears to be invalid. Please hit your browser back button
to return to the previous page to enter a vaild email address.</p>';
return;
}
$phone = stripslashes( strip_tags( $_POST['phone'] ) );
$msg_body = "Order placed for:
$order
Total: $$total
Name: $name
Email: $email
Phone: $phone";
@mail($admin_email, $subject, $msg_body );
}
function handleOrderInfo() {
global $PRODUCTS;;
$str = ''; $total = 0; $order = '';
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'); // get product abbr
$name_pt = substr( $key, 0, $pt);
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;
$order .= "$val $prod_abbr, ";
}
}
}
}
$total = number_format($total, 2);
$order = rtrim($order, ', ');
if ( $str === '' ) {
$str = '<p>You didn\'t order anything.</p>';
} else {
$str = "<h2>Your Order:</h2>$str<p>Total: $$total</p>";
sendAdminEmail($total, $order);
}
return $str;
}
?>
Download the order form examples.
Back to Order Form | Top