This code is a slighlty more advanced example of a dbForm.
The parts of note are:
- alwaysUpdate
Forces the record to be updated on select or new actions - setFieldTitle
Sets the field titles for fields. Note that the field titles are now all Uppercase first letter and followed by colons (:). This is done with 2 commands only for the sake of example. - setFieldAttributes
Sets attributes of fields.
The first command sets all fields to have a style that makes them display in red, while the second overwrites this style attribute for the searchid and instead sets the fields width to 400 pixels. - setButtonAttributes
Sets attributes on buttons.
This command is being used to make all of the buttons appear the same width.
demo2.php
<?php
/*
* Intermediate Demonstration
*
* Example of using dbForm to display
* and process a web-form for a database table.
*
* Uses a person table with the following MySQL structure
* CREATE TABLE `person` (
* `personid` int(11) NOT NULL auto_increment,
* `firstname` varchar(50) default NULL,
* `lastname` varchar(50) default NULL,
* `username` varchar(20) default NULL,
* `password` varchar(32) default NULL,
* `notes` text,
* `accessid` int(11) default '1',
* `active` tinyint(1) unsigned default '1',
* `canceldate` date default NULL,
* PRIMARY KEY (`personid`),
* UNIQUE KEY `personid` (`personid`),
* KEY `personid_2` (`personid`)
* ) TYPE=MyISAM COMMENT='person table demo';
*
* and the access table with the following MySQL structure and data
* CREATE TABLE `access` (
* `accessid` tinyint(3) NOT NULL auto_increment,
* `name` varchar(20) default NULL,
* PRIMARY KEY (`accessid`),
* UNIQUE KEY `accessid` (`accessid`),
* KEY `accessid_2` (`accessid`)
* ) TYPE=MyISAM COMMENT='demo access table';
*
* INSERT INTO access VALUES("1", "Admin");
* INSERT INTO access VALUES("2", "User");
*
*/
// include the necessary files
include_once("/path/to/adodb/adodb.inc.php");
include_once("/path/to/dbform/formFields.inc");
include_once("/path/to/dbform/template.inc");
include_once("/path/to/dbform/dbForm.inc");
// create a database connection
$conn = &ADONewConnection('mysql');
$conn->Connect('localhost', 'username', 'password', 'datasource');
$conn->debug = false;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$dbForm = new dbForm("Person", $conn, "person");
// always Update the database
// forces update on selection or new
$dbForm->_alwaysUpdate = true;
$dbForm->_hashPasswords=true;
// true parameter forces autoSelection
$dbForm->setSearchField("personid","select personid, lastname, firstname from person order by lastname, firstname", ", ", "Select a Person", true);
$dbForm->changeFieldType("password","password");
$dbForm->changeFieldType("accessid","select");
$dbForm->setCheckbox("active",1,0);
// set the Date max and min
$dbMinYear = 1950;
$dbMaxYear = 2050;
// set field Titles
$dbForm->setFieldTitle("firstname","Firstname");
$dbForm->setFieldTitles(array("lastname"=>"Lastname:","username"=>"Login name:","password"=>"Password:","notes"=>"Notes:","accessid"=>"Access Level:","active"=>"Active:","canceldate"=>"Cancel Date:"));
// set field Attributes
$dbForm->setAllFieldsAttributes(array("style"=>"color:red"));
$dbForm->setFieldAttributes("dbform_searchid",array("style"=>"width:400px"));
// set button Attributes
$dbForm->setAllButtonsAttributes(array("style"=>"width:75px"));
$dbForm->processForm();
$dbForm->getSelectValues("accessid","select accessid, name from access");
$dbForm->setTemplateFile("demo2.tpl");
$dbForm->displayForm();
?>
This template is basically the same as the one used by the Basic Demo, however it does include the javascript file for the autoSelection
demo2.tpl
<script language="javascript" src="/dbForm.js"></script>
{:form}
<table border="1" width="500" align="center">
<caption>{:FORMNAME}</caption>
{:action="null,select,insert,update,delete,cancel"}
<tr>
<td width="100">Search:</td>
<td width="400">{:FIELD="dbform_searchid"}</td>
</tr>
<tr>
<td colspan="2" align="right"><font color="red" size="-2"> {:status}</font></td>
</tr>
{:end action}
<tr>
<td colspan="2" align="center">
{:action="null,select,insert,update,delete,cancel"}
{:BUTTON="new"}
{:end action}
{:ACTION="select,insert,update"}
{:BUTTON="update"}
{:button="delete"}
{:END ACTION}
{:ACTION="new"}
{:button="insert"}
{:button="cancel"}
{:END ACTION}
</td>
</tr>
{:ACTION="new,insert,select,update"}
{:EACH FIELD}
<tr>
<td width="100">{:fieldname}</td>
<td width="400">{:field}</td>
</tr>
{:END FIELD}
{:end action}
</table>
{:hidden}
</form>
The files that have been used in this demonstration can be downloaded so you can try them out for yourself.