HTML form building may pose problems for unesperienced people because there are many little and subtle things to know; even experienced people may find confortable to have a more consistent interface. Since long ago I've trying to build a class that would help that process. The idea actually came from a guy that was working for us at the time. Nothing new, there are many classes to build forms, but not even one in the way I wanted, at the time.
My form class had to help to build form controls, to retrieve their value and to build database queries to give the best efficiency and to reduce bugs. It started to support MySQL, then we had to do a job with Oracle and so we expanded it to handle Oracle too. After starting the free Obliquid project I switched to Metabase to be able to support different databases without having to write special code for any of them.
The most basic way to use the db_form class is without the database: even if this is a rare case it will help to grasp the concepts in a first step. I usually form controls in a form array that I call $frm that I then pass to the Smarty template. In this example I simply build a form with the available controls.
getTextInput method returns a text box input, the first parameter name is the html variable name that will be stored in $_POST["name"] after submitting the form. All other parameters are optional, but typically the SIZE would be specified, as the CSS class "textform". Having a common CSS class helps: if we want to change the font sizes for forms in a theme we can do by modifying a single value.
getTextArea method returns a text area. In HTML forms the basic syntax of a textArea is different from the syntax of a text box, while in db_form such difference disappear. The first parameter of the method is, as usual, the variable name, description in this case; after this there is an optional array of additional parameters.
getDateInput helps to input dates: as always the first parameter is the variable name, in this case sdate and the other parameters are optional, the start year is the first optional parameter 1998 in our example, which is also the default, the second parameter is the end year, by default it's the current year, but here we defined it to be two years later date("Y")+2, as last parameter the textform style is applied.
getCheckbox returns a HTML checkbox. As always the first parameter is the variable name ( restricted ) and it's the only one used in this example.
getRadio returns a radio button, the first parameter is th variable name, in this case recur. As you can notice, there are two radio buttons with the same name, and it means they are mutually exclusive.
getSubmit returns a submit button: the first parameter is the variable name, which value is usually not considered, since it's its label.
PHP excerpt
$dbf =& new db_form();
$frm["name"]=$dbf->getTextInput("name",
array("CLASS" => "textform", "SIZE" => "25"));
$frm["description"]=$dbf->getTextArea("description",
array("CLASS" => "textform", "ROWS" => "3", "COLS" => "45"));
$frm["moddate"]=$dbf->getDateInput("moddate", 1998, date("Y")+2,
array("CLASS" => "textform"));
//radio buttons must start with a default, for the html specification
$frm["author_no"]=$dbf->getRadio("author", "");
$frm["author_st"]=$dbf->getRadio("author", "Stefano");
$frm["author_ni"]=$dbf->getRadio("author", "Nick");
$frm["enable"]=$dbf->getCheckbox("enable");
$frm["version"]=$dbf->getSelect("version", array(array("1.0", "stable"),
array("beta", "beta"),
array("alpha", "alpha")
));
$frm["submit"]=$dbf->getSubmit("submit", _l("Save"), array("CLASS" => "textform"))
.$dbf->getHidden("action", "exampleform");
$_obweb->smslot->assign(array("frm" => $frm));
Smarty template
<form method="post" NAME="exampleform">
Module name: {{$frm.name}}<br><br>
Description: {{$frm.description}}<br><br>
Last modified: {{$frm.moddate}}<br><br>
Author: Not chosen {{$frm.author_no}}
Stefano {{$frm.author_st}}
Nick {{$frm.author_ni}}<br><br>
Enabled: {{$frm.enable}}<br><br>
Version: {{$frm.version}}<br><br>
{{$frm.submit}}
</form>