Martyn Bissett


Tutorials | Form Class

This is a really useful class that I created myself but was inspired by ASP.NET's View State feature that it uses with form elements to ensure that when the form is reloaded after submission (perhaps an error was found and the user needs to review and re-submit) it will populate the form fields. In the past I would have done something like this:

<input type="text" name="make" value="<?php

if(isset($_POST['make'])) {
  echo $_POST['make'];
} else {
  echo 'Toyota'; // default value
}

?>" />

If you have many elements on your form this can be a real pain plus its not very pretty to look at. My idea uses the following to genarate the HTML for an input box:

<?php Form::TextBox('make', 'Toyota'); ?>

As you can see it is much neater and this works a breeze when having to build many elements within many forms. It also works very well within my PHP templates discusses in the PHP Template Files tutorial.

Lets look now at the Form class:

Class Form {
  public function TextBox($name, $defaultValue = null) {
    echo '<input type="text" name="'.$name.'" id="'.$name.'" value="';
    if(isset($_POST[$name])) {
      echo $_POST[$name];
    }else{
      echo $defaultValue;
    }
    echo '" />';
  }
}

This is only showing an example of one form element, text box, but similar methods can cater for all sorts of elements such as radio buttons, check boxes, passwords, etc. Below shows a form within a template with multiple elements:

<form>
<label for="make">Make: </label>
<?php Form::TextBox('make'); ?>
<label for="model">Model: </label>
<?php Form::TextBox('model'); ?>

<fieldset>
  <legend>Colour</legend>
  <label for="colour">Red: </label>
  <?php Form::RadioButton('colour', 'Red'); ?><br />
  <label for="colour">Green: </label>
  <?php Form::RadioButton('colour', 'Green'); ?><br />
  <label for="colour">Blue: </label>
  <?php Form::RadioButton('colour', 'Blue'); ?><br />
</fieldset>

<label for="description">Description: </label><br />
<?php Form::TextArea('description'); ?>

Ofcourse different elements would be constructed in different ways (eg. you wouldn't give a group of same name radio buttons the same ID attribute) and you may pass different arguments to do different stuff for your application but the main thing is that view state is maintained and we can create forms very quickly and neatly.

This is the basis of the Form class but in proceeding section I will discuss how we can extend the same class to cater for list elements using XML.

Creating lists using XML

Lists have predefined values and we should also use what benefits we have gained from the class so far with out lists. If we wanted to create the following list:

<select name="make">
  <option value="Toyota">Toyota</option>
  <option value="Fiat">Fiat</option>
  <option value="MG" selected="selected">MG</option>
</select>

First we should enter this data into an XML file:

<?xml ... ?>
<select name="make">
  <option value="Toyota">Toyota</option>
  <option value="Fiat">Fiat</option>
  <option value="MG">MG</option>
</select>
Create a new function in the Form class:
public function SelectFromXML($file, $defaulValue = null) {
  $Xml = simplexml_load_file($file);
  $name = $Xml['name'];
  echo '<select name="'.$name.'">';
  foreach($Xml->option as $Option) {
    echo '  <option value="'.$Option['value'].'"';
    if(isset($_POST[$name])) {
      if(“$Option" == $_POST[$name]) {
        echo ' selected="selected"';
      }
    } else if(! is_null($defaultValue)) {
      if(“$Option" == $defaultValue) {
        echo ' selected="selected"';
      }
    }
    echo '>'.$Option.'</option>';
  }
  echo '</select>';
}

So what the above process is doing is parsing the XML (Ive used SimpleXML here but any form of PHP XML parsing should do) and outputting the options of the select file based on the pre-defined values in the XML file. Notice also how we do a similar method of maintaining view state as with the text box but this time we're comparing values and if we get a match - echo ' selected="selected"'

So to output this on our template we would do the following:

Form::SelectFromXML('make.xml', 'MG');