PHP Order Form Documentation

This page provides documentation for the PHP order forms. First we show how the order forms are generated using the PHP Form Class and PHP Table Class. Then we cover data you will need to enter in the code in order to test or use the order forms.[1]

The Products Array

The example order forms use the following products array:

$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)

Product names and prices are displayed in the order forms. Product abbreviations are used in form element names and should follow naming conventions.

Using Table and Form Class Methods Together

The function displayed below outputs an order form using data in the products array and the methods provided by the form and table classes. First, instances of HTML_Table and HTML_Form are created. Next, the header row is created using the table class addCell method. Then form elements are generated using the form class addInput method. These form elements are placed in table cells using the table class addCell method.

function getOrderForm() {
    global $PRODUCTS;
    // create instances of table and form classes
    $tbl = new HTML_Table('', 'demoTbl');
    $frm = new HTML_Form();
    // add header row
        $tbl->addCell('Product', 'first', 'header');
        $tbl->addCell('Price', '', 'header');
        $tbl->addCell('Quantity', '', 'header');
        $tbl->addCell('Totals', '', 'header');
    // display product info/form elements
    foreach($PRODUCTS as $product) {
        list($abbr, $name, $price) = $product;
        // quantity text input
        $qty_el = $frm->addInput('text', $abbr . '_qty', 0, 
            array('size'=>4, 'class'=>'cur',
                  // html5 pattern and placeholder attributes
                  'pattern'=>'[0-9]+', 'placeholder'=>0, 
                  'onclick'=>'checkValue(this)', 'onblur'=>'reCheckValue(this)') );
        // total text input
        $tot_el = $frm->addInput('text', $abbr . '_tot', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') );
        // price hidden input
        $price_el = $frm->addInput('hidden', $abbr . '_price', $price);
        // add form elements to table cells
            $tbl->addCell('$' . number_format($price, 2) . $price_el, 'cur' );
            $tbl->addCell( $qty_el, 'qty');
            $tbl->addCell( $tot_el );
    // total row
        $tbl->addCell( 'Total: ', 'total', 'data', array('colspan'=>3) );
        $tbl->addCell( $frm->addInput('text', 'total', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
    // submit button
        $tbl->addCell( $frm->addInput('submit', 'submit', 'Submit'),
                'submit', 'data', array('colspan'=>4) );
    $frmStr = $frm->startForm('ex1_result.php', 'post', '', array('onsubmit'=>'return checkSubmit(this);') ) .
        $tbl->display() . $frm->endForm();
    return $frmStr;

Documentation for the form and table classes provide details on their use.


In order to test or use the example order forms, you would need to provide your information for a few variables. Example one includes a function to output a standard PayPal button. The getPayPalBtn function includes variables for your account email, a return url, and a cancel url:

function getPayPalBtn($total) {
    // your PayPal account email
    $paypal_email = '';
    // (more of the PayPal button code here...)
    // page to send customer after PayPal payment
    $return_url = '';
    // if user cancels rather than paying
    $cancel_url = ''; 
    // ...

Order form example two emails order information to the address you specify in the variable $admin_email:

function sendAdminEmail($total, $order) {
    // place your email address here
    $admin_email = '';
    // ...

If you are unable to send emails on your server, you would need to check into the server settings and requirements. For example, some servers may require a from address to be provided in the headers argument of the mail function.[2]

Download Code - It's Free!

The order form examples and the PHP Form Class and PHP Table Class used to generate them are provided in a download file free of charge.

Back to top

  1. These order forms are presented as basic examples. They should not be assumed to be suitable for use in accepting payments on a live web site without prior review by a security professional. ^
  2. See the PHP Manual for information on the mail function additional headers argument. ^