Generate forms programmatically
Auto form class can generate simple insert, update, select and delete HTML forms, form validation and form processing code programmatically based on mysql table information
It classifies mysql data types in different categories, uses predefined HTML markup for each of them, and uses standard datatype attributes like legth, null, etc for input validation generation
User can exclude table columns from appearing in forms, can add custom html code, add custom input elements and also provide custom validation functions
User can also provide callback functions for successful form submission, for incorrect submission (user input didn't pass validation), or to modify submited values
It is possible to provide language array, where keys are mysql table column names and values are labels to show near input form. If language array is not provided, column names will be used instead.
This package contains multiple examples to help you grasp the concept of this class. Simple example contains simple insert and update forms, to show multiple forms working on same page. Other examples contain user account manipulation forms, like registering, login in, editing account info and deleting account, so you can see how with little amount of code using Auto form class you can create forms with different purpose.
Contents
- Download
- Example codes
- Examples in action
- Method list
- Validation types
- HTML inputs for Mysql datatypes
- CSS styles for input elements
- Latest changes
- Rate us
- Support
Download
Example codes
<?php ob_start(); //database connection include("./connection.php"); //form class include("./auto_form.php"); //on success function success($result) { if($result === false) { echo "<p>No connection</p>"; } else if($result === 0) { echo "<p>No id generated</p>"; } else { echo "<p>New id is ".$result."</p>"; } } $form = new auto_form($serverlink); $form->debug(); //exclude table columns $ex = array("ID"); //language array $lang = array( "int" => "Integer", "decimal" => "Decimal", "double" => "Float", "text" => "Text", "bool" => "Checkbox", "enum" => "Select", "set" => "Multiple select", "varchar" => "Default"); $form->set_language($lang); //set on success function $form->set_onsuccess("success"); //insert form echo "<fieldset>"; echo "<legend>Insert form</legend>"; $form->insert_form("test_table", $ex); echo "</fieldset>"; //update form echo "<fieldset>"; echo "<legend>Update form</legend>"; $form->update_form("test_table", "WHERE ID = '1'", $ex); echo "</fieldset>"; ob_end_flush(); ?>
Examples in action
Example scripts provided with package in action:
Method list
- Constructor
- Enable validation
- Disable validation
- Add custom validation
- Allow/disallow HTML tags
- Provide custom language
- Debug
- On succes callback
- On error callback
- Modify submitted values
- Add custom html
- Add class predefined validation
- Generate insert form
- Generate update form
- Generate select form
- Generate delete form
Constructor
| Method name | new auto_form($con); |
| Description | Create class instance. Mysql connection link needs to be provided, which will be used to execute generated queries |
| Input parameters | resource $con - MySQL link identifier from opened msql connection |
Enable validation
| Method name | enable_validation() |
| Description | Enable programatically generated validation for all following forms. By default validations is enabled, but you may want to disable it for some forms and enable for others, if you have multiple forms on one page |
Disable validation
| Method name | disable_validation() |
| Description | Disable programatically generated validation for all following forms. By default validations is enabled. Custom validations will still be applied |
Add custom validation
| Method name | add_custom_validation($callback, $error_str) |
| Description | Add callback function to custom validation. Callback function should accept array of all form submited values, where keys are mysql table column names. Callback function should return true if submission was valid and false if invalid. User may provide multiple custom validation functions using this method. Using custom validation only error text will be passed in array to on error callback functions, without element name or id |
| Input parameters | string or array $callback - name of callback functions or array with instance and method (parameters that are supported by "call_user_func" function) string $error_str - error string to show if submitted values didn't pass validation |
Allow/disallow HTML tags
| Method name | allow_html($html) |
| Description | Allows or disallows HTML tag usage in form (converts to html entities if HTML tags are not allowed) Currently this method applies to whole form |
| Input parameters | bool $html - true to allow HTML tags and false to disallow |
Provide custom language
| Method name | set_language($lang) |
| Description | Provides custom language for input labels and error messages For input labels, use mysql table column names as keys. and for error messages, use "colum nname"_"error type". Check validation types section for possible error type values |
| Input parameters | array $lang - language array |
| Example input | set_language(array("username" => "Username", "pass" => "Password", "pass_required" => "You must enter password")) |
Debug
| Method name | debug() |
| Description | Outputs all mysql query texts and errors |
On succes callback
| Method name | set_onsuccess($callback) |
| Description | Set callback function which will be called, if form is successfully submited (user input passes form validation) Callback function should accept one parameter. Parameter type depends on form type.
Callback function should return true to allow page refresh or provided redirection, or false to not refresh or redirect page User can submit only one on success callback function per form. Previous function will be overwritten |
On error callback
| Method name | set_onerror($callback) |
| Description | Set callback function which will be called, if form is submited with errors (user input didn't pass validation) Callback function should accept one parameter - two dimensional array with error texts, input name and HTML element input id (for javascript error displaying), except if it was error from custom validation, then only error text will be provided in array, without name and id If on_error function isn't provided, then class will automatically print out errors, if function is set, then no errors will be print out User can submit only one on error callback function per form. Previous function will be overwritten |
| Example passed parameter | Example of array passed to callback function Array ( [0] => Array ( ["id"] => username_0 ["name"] => username ["error"] => Please enter the value for input username ) [1] => Array ( ["id"] => pass_0 ["name"] => pass ["error"] => Please enter the value for input pass ) ) |
Modify submitted values
| Method name | set_modification($callback) |
| Description | Set callback function which will be called after form validation (if user input passes form validation), but before forming query text and use it on mysql table Callback function should accept one parameter - array with column names as keys and submitted values as values User can submit only one modification callback function per form. Previous function will be overwritten |
| Example passed parameter | Example of array passed to callback function Array ( ["username"] => my_user_name ["password"] => my_pass ["captcha"] => 4FTG3 ) |
Add custom html
| Method name | add_custom_html($html, $name = "", $in_query = false, $hidden = false) |
| Description | " Add custom HTML code to form. HTML code as any input in form will be wrapped to new table row and table data HTML elements. Provided HTML code maybe custom input or just for decoration purpose If name of input element is provided, then one td cell will be created for input label from language array or column name and other for input field, just like any other input element. To avoid it provide parameter $hidden as true |
| Input parameters | string $html - string with HTML markup string $name - name of input element if input element is provided, default value - empty bool $in_query - should value submitted through this input element appear in generated query using input name as column name in mysql table, if it is input element. If true, value will be included in query. Default value is false bool $hidden - use if you don't want class to create label for your provided input (just like hidden form elements) |
Add class predefined validation
| Method name | add_class_validation($name, $group, $required = true, $maxlen = "") |
| Description | Add class predefined validation to input elements provided with add_custom_html method. Predefined validation is based on input type and required/maxlen values |
| Input parameters | string $name - name of input element to validate (same value provided to add_custom_html method) string $group - mysql data type group name from HTML inputs for mysql datatypes bool $required - if should be validated as required from Validation types. Default value - false int $maxlen - maximal length of value interpreted as string from Validation types. Default value empty (will not be counted) |
Generate insert form
| Method name | insert_form($table_name, $exclude = array(), $redir = "", $submit = "submit") |
| Description | Generate form, which will insert new data to database |
| Input parameters | string $table_name - name of mysql table for which form is generated array $exclude - array with column names as values, which columns should be excluded from form string $redir - where to redirect after successful form submission using header("Location:"). Default value is empty, so page will be refreshed. Page won't be redirected or refreshed if submitted values didn't pass form validation or if on success callback function returns false, or if output already started and headers can not be sent string $submit - value for submit button (Text that appears on button) |
Generate update form
| Method name | update_form($table_name, $condition, $exclude = array(), $redir = "", $submit = "submit") |
| Description | Generate form, which will update existing data in database |
| Input parameters | string $table_name - name of mysql table for which form is generated string $condition - what rows to update, mysql where clause (for example: WHERE ID = '1') array $exclude - array with column names as values, which columns should be excluded from form string $redir - where to redirect after successful form submission using header("Location:"). Default value is empty, so page will be refreshed. Page won't be redirected or refreshed if submitted values didn't pass form validation or if on success callback function returns false, or if output already started and headers can not be sent string $submit - value for submit button (Text that appears on button) |
Generate select form
| Method name | select_form($table_name, $exclude = array(), $addon = "", $redir = "", $submit = "submit") |
| Description | Generate form, which will select existing data from database which matches user provided column values Select query will be generated from form input element values |
| Input parameters | string $table_name - name of mysql table for which form is generated array $exclude - array with column names as values, which columns should be excluded from form string $addon - additional conditions for query. Where clause will be automatically generated using pairs from input name and input value. It is possible to provide additional condition, like order, limit, offset, group (any condition that can go fter where clause in mysql query). You must provide valid addon, for query to work. Use debug() method to check mysql query texts and errors string $redir - where to redirect after successful form submission using header("Location:"). Default value is empty, so page will be refreshed. Page won't be redirected or refreshed if submitted values didn't pass form validation or if on success callback function returns false, or if output already started and headers can not be sent string $submit - value for submit button (Text that appears on button) |
Generate delete form
| Method name | select_form($table_name, $exclude = array(), $redir = "", $submit = "submit") |
| Description | Generate form, which will delete existing data in database which matches user provided column values Delete query will be generated from form input element values |
| Input parameters | string $table_name - name of mysql table for which form is generated array $exclude - array with column names as values, which columns should be excluded from form string $redir - where to redirect after successful form submission using header("Location:"). Default value is empty, so page will be refreshed. Page won't be redirected or refreshed if submitted values didn't pass form validation or if on success callback function returns false, or if output already started and headers can not be sent string $submit - value for submit button (Text that appears on button) |
Validation types
List of validation types
| Type name | What validates | Default error text | Used if |
| required | If value is not empty Except bool type inputs, where it checks whether checkbox was checked | Please enter the value for input [input name] | Mysql column is set to NOT NULL |
| maxlen | Length of value, if it is not greater then maximum value. Validation is also applied to form input element as maxlength attribute if possible | Maximum length exceeded for input [input name] | Number of characters taken from values length, like varchar(10), where maxlength is 10 is specified (validation differs for different mysql data types) |
| int | If value is integer | Please provide integer value for input [input name] | Column type is int, bigint, etc |
| dec | If value is decimal and total length doesn't exceed length specified in mysql and number of digits to the right of the decimal point, doesn't exceed length specified in mysql (decimal(5,2)). Comma will be automatically converted to point. | Please provide decimal value for input [input name] | Column type is decimal |
| float | If value is float. Comma will be automatically converted to point. | Please provide float value for input [input name] | Column type is float, double |
| enum | If selected value is defined in mysql table for enum or set types. Error may happen, when user modifies HTML code values of select element | Incorrect value selected for input [input name] | Column type is set or enum |
HTML inputs for Mysql datatypes
HTML code for different mysql data types
So basically all mysql datatypes are divided in groups, and for each group there is a special form input and specific validations applied
Every form is wrapped in table. Each input is in it's own table row.
| Input group | Datatypes used in group | HTML codes | Possible validation types |
| int | int, smallint, mediumint, bigint, tinyint(with size greater than 1) | <tr> <td class='int_label'> [columnname or language]: </td> <td class='int_input'> <input type='text' name='[columnname]_[formnum]' id='[columnname]_[formnum]' maxlength='[length]' value='[value]'/> </td> </tr> | required, maxlen, int |
| decimal | decimal | <tr> <td class='decimal_label'> [columnname or language]: </td> <td class='decimal_input'> <input type='text' name='[columnname]_[formnum]' id='[columnname]_[formnum]' maxlength='[length]' value='[value]'/> </td> </tr> | required, maxlen, dec |
| float | float, double | <tr> <td class='float_label'> [columnname or language]: </td> <td class='float_input'> <input type='text' name='[columnname]_[formnum]' id='[columnname]_[formnum]' maxlength='[length]' value='[value]'/> </td> </tr> | required, maxlen, float |
| text | tinytext, text, mediumtext, longtext, tinyblob, mediumblob, blob, longblob | <tr> <td colspan='2' class='text_label'> [columnname or language]: </td> </tr> <tr> <td colspan='2' class='text_input'> <textarea name='[columnname]_[formnum]' id='[columnname]_[formnum]'> </textarea> </td> </tr> | required |
| bool | tinyint(1) - size exactly 1 | <tr> <td colspan='2' class='bool_input'> <input type='hidden' name='[columnname]_[formnum]' value='0'/> <input type='checkbox' name='[columnname]_[formnum]' id='[columnname]_[formnum]' value='1' /> [columnname or language]: </td> </tr> | required (force user to select checkbox) |
| enum | enum | <tr> <td class='enum_label'> [columnname or language]: </td> <td class='enum_input'> <select name='[columnname]_[formnum]' id='[columnname]_[formnum]'> <option value='[defined_value]'> [defined_value or language] </option> </select> </td> </tr> | required, enum |
| set | set | <tr> <td class='set_label'> [columnname or language]: </td> <td class='set_input'> <input type='hidden' name='[columnname]_[formnum]' value=''/> <select name='[columnname]_[formnum][]' id='[columnname]_[formnum]' multiple='multiple' size='3'> <option value='[defined_value]' > [defined_value or language] </option> </select> </td> </tr> | required, enum |
| char | any other mysql datatype | <tr> <td class='char_label'> [columnname or language]: </td> <td class='char_input'> <input type='text' name='[columnname]_[formnum]' id='[columnname]_[formnum]' maxlength='[length]'/> </td> </tr> | required, maxlen |
columnname - Mysql table column name
language - User provided language array
formnum - Form number created by class instance on one page (starting from 0)
length - Size of field in characters from mysql table definition
value - Value of field if specified
defined_value - Value defined in mysql table as possible values for enum or set types
CSS styles for input elements
There ae some CSS classes and element IDs automatically generated with HTML form for easier form designing
| HTML element | Styles and IDs | Examples |
| <form> | ID - [mysql table name]_[formnum] | <form id='test_table_1'> |
| <table> | CSS class - [mysql table name] | <table class='test_table'> |
| <td> | CSS class for td with text labels - [type group]_label CSS class for td with input element - [type group]_input | <td class='int_label'>Username: </td> <td class='int_input'><input type='text' name='username_1' id='username_1' value='1' maxlength=''/></td> <td class='submit_input' colspan='2'><input type='hidden' name='test_table_1'/><input type='submit' id='submit_1' value='Submit'/></td> |
| <input> or <textarea> or <select> | ID - [columnname]_[formnum] | <input type='text' name='username_1' id='username_1' value='1' maxlength=''/> |
| <input type='submit'> | ID - submit_[formnum] | <input type='submit' id='submit_1' value='Submit'/> |
Example generated form with css styles and IDs:
<form id='test_table_1' method='post' action=''> <table class='test_table'> <tr> <td class='int_label'> Username: </td> <td class='int_input'> <input type='text' name='username_1' id='username_1'/> </td> </tr> <tr> <tr> <td colspan='2' class='text_label'> Comment: </td> </tr> <tr> <td colspan='2' class='text_input'> <textarea name='comment_1' id='comment_1'> </textarea> </td> </tr> <tr> <td colspan='2' class='bool_input'> <input type='hidden' name='agree_1' value='0'/> <input type='checkbox' name='agree_1' id='agree_1' value='1' checked/> Agree to toss </td> </tr> <tr> <td class='enum_label'> Select type: </td> <td class='enum_input'> <select name='type_1' id='type_1'> <option value='spam' >Spam</option> <option value='pingback' selected>Pingback</option> <option value='comment' >Comment</option> </select> </td> </tr> <tr> <td class='submit_input' colspan='2'> <input type='hidden' name='test_table_1'/> <input type='submit' id='submit_1' value='submit'/> </td> </tr> </table> </form>
Latest changes
None for now
Rate us
Try it out and Rate on PHPclasses.org
Support
PHP classes support forum or comments below
You may also be interested in:
Powered by BlogAlike.com










