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.

<?php
function getOrderForm() {
    global $PRODUCTS;
    
    // create instances of table and form classes
    $tbl = new HTML_Table('', 'demoTbl');
    $frm = new HTML_Form();
    
    // add header row
    $tbl->addRow();
        $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, 
                  'onchange'=>'getProductTotal(this)',
                  '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->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( 'Total: ', 'total', 'data', array('colspan'=>3) );
        $tbl->addCell( $frm->addInput('text', 'total', 0, array('readonly'=>true, 'size'=>8, 'class'=>'cur') ) );
    
    // submit button
    $tbl->addRow();
        $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.

Customization

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 = 'your_paypal_email@your.com';
    
    // (more of the PayPal button code here...)
    
    // page to send customer after PayPal payment
    $return_url = 'http://www.your_url.com/orders/thankyou.html';
    
    // if user cancels rather than paying
    $cancel_url = 'http://www.your_url.com'; 
    // ...

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 = 'your_addy@your.com';
    // ...
}

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. ^