Source code in Qf2Simple.php

To see how this is used return to this tutorial index.

<?php
    // A simple quickform. Return to this tutorial index to see it working.

    // Copyright (c) 2009, Parliament Hill Computers (www.phcomp.co.uk). Author: Alain D D Williams (addw@phcomp.co.uk).
    // You may used this file as the basis your own (or organisation's/company's) project (under whatever
    // licence that you see fit), but may not claim ownership or copyright of the substantially unmodified file.
    // This file is made available in the hope that it is useful, there is no warranty at all, use at your own risk.

    // The idea of these 4 scripts is to generate the same web pages using QuickForm2 as their equivalents
    // that use QuickForm and use the same smarty template (when smarty is used).
    // The equivalent for this one is QfSimple.php

    // The comments show what is changed, the change comments are usually only given once - read in order.

    // SCCS: %W% %G% %U%
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
    <title>Quickform Demo</title>
    <link rel="stylesheet" type="text/css" href="/DefaultStyle.css">
    <!-- You need a stylesheet to make it look like the old QF -->
    <link rel="stylesheet" type="text/css" href="Qf2Style.css">
 </head>

 <body>
  <!-- A standard page header -->
  <div style='position : absolute;text-align: left;top:10px;'>
   <a href='/'><img src='/Images/kite_40_83_trans.gif' width="40" height="83" alt="Parliament Hill Computers Ltd" style="border-width: 0;"></a>
  </div>
  <div style='top:0px;margin:0px auto;text-align: center; font-size:40pt; '>
   Simple Quickform
  </div>
  <p>

<?php

    // Bring in the new QuickForm2 & create the appropriate object:
    require_once "HTML/QuickForm2.php";

    // Create a GET form with the name QfDemo:
    $form = new HTML_QuickForm2('QfDemo', 'get');

    // addElement() adds a display line or an input field to the form.
    // The first argument is a standard html type (text, checkbox, hidden, ...) or a custom
    // type (header, group, ...). Other arguments depend on the type.

    // QF2 doesn't use headers, but we can put in a hidden field
    // $form->addElement('header', 'DemoHeader', 'Quickform Demo');
    $form->addElement('hidden', 'DemoHeader')->setValue('Quickform Demo');

    // Add a drop down select list:
    $Titles = array('Please select', 'Mr', 'Mrs', 'Miss', 'Dr');
    $form->addElement('select', 'Title', null, array('label' => 'Title', 'options' => $Titles));

    // Could also have done:
    //    $title = $form->addElement('select', 'Title');
    //    $title->setLabel('Title');
    //    $title->loadOptions($Titles);
    // or:
    //    $form->addElement('select', 'Title')->setLabel('Title')->loadOptions($Titles);

    // Add a text field, the form element name is 'FirstName' with a title 'First name':
    // $form->addElement('text', 'FirstName', 'First name');
    $form->addElement('text', 'FirstName', null, array('label' => 'First name'));

    // A few other fields:
    // $form->addElement('text', 'LastName', 'Last name');
    $form->addElement('text', 'LastName', null, array('label' => 'Last name'));

    // $form->addElement('text', 'Age', 'Age');
    $form->addElement('text', 'Age', null, array('label' => 'Age'));
    
    // $form->addElement('text', 'Telephone', 'Telephone number');
    $form->addElement('text', 'Telephone', null, array('label' => 'Telephone number'));

    // Standard buttons, specify the text in the Submit button:
    // $form->addElement('reset', 'Clear', 'Clear');
    $form->addElement('reset', 'Clear');

    // $form->addElement('submit', 'Submit', 'Submit');
    $form->addElement('submit', 'Submit', array('value' => 'Press to Submit'));

    // First time round this results in false, remember that, as is common in php, this
    // script is used both to generate the form and to process the results.
    if ($form->validate()) {
        # Get here when the form has been filled in
        // $form->freeze();	// If we want the form redisplayed in the way that the user entered it
        // but you need to do another display()
        // $res = $form->exportValues();
        $res = $form->getValue();
        echo "Title = " . htmlspecialchars($res['Title']) . " = '" . $Titles[$res['Title']] . "'<br>\n";
        echo "FirstName = " . htmlspecialchars($res['FirstName']) . "<br>\n";
        echo "LastName = " . htmlspecialchars($res['LastName']) . "<br>\n";
        echo "Age = " . htmlspecialchars($res['Age']) . "<br>\n";
        echo "Telephone = " . htmlspecialchars($res['Telephone']) . "<br>\n";
    } else {
        // First time display the form
        // $form->display();
        echo $form;
    }
?>

  <p>
   This is a simple form, fill in some values and press <i>Submit</i>.

  <p style="font-size: x-large; font-weight: bold;">
   Return to this <em><a href='index.php'>tutorial index</a></em>.
  </p>

 </body>
</html>

Return to this tutorial index.