Recommend this page to a friend! |
Classes of Manuel Lemos | PHP Forms Class with HTML Generator and JavaScript Validation | README.md | Download |
|
DownloadPHP Form ExampleInclude the forms class directly or use vendor/autoload.php if you install it with composer.
Create a form object.
Define the name of the form to be used for example in Javascript validation code generated by the class.
Use the GET method if you want to see the submitted values in the form processing URL, or POST otherwise.
Make the form be displayed and also processed by this script.
Specify a debug output function you really want to output any programming errors.
Define a warning message to display by Javascript code when the user attempts to submit the this form again from the same page.
Output previously set password values.
Output multiple select options values separated by line breaks
Output all validation errors at once.
CSS class to apply to all invalid inputs. Set to a non-empty string to specify the invalid input CSS class
Style to apply to all invalid inputs when you just want to override a few style attributes, instead of replacing the CSS class. Set to a non-empty string to specify the invalid input style attributes
Text to prepend and append to the validation error messages.
Define the form field properties even if they may not be displayed.
PHP Form ValidationLoad form input values eventually from the submitted form.
Empty the array that will list the values with invalid field after validation.
Check if the was submitted as opposed to being displayed for the first time.
Therefore we need to validate the submitted form values.
It's valid, set the $doit flag variable to 1 to tell the form is ready to processed.
It's invalid, set the $doit flag to 0 and encode the returned error message to escape any HTML special characters.
The form is being displayed for the first time, so it is not ready to be processed and there is no error message to display.
The form is ready to be processed, just output it again as read only to display the submitted values. A real form processing script usually may do something else like storing the form values in a database.
If the form was not submitted or was not valid, make the page ONLOAD event give the focus to the first form field or the first invalid field.
If there is at least one field with invalid values, get the name of the first field in error to make it get the input focus when the page is loaded.
Make the email field get the input focus when the page is loaded if there was no previous validation error.
Connect the form to the field to get the input focus when the page loads.
PHP HTML Form Example Output
Compose the form output by including a HTML form template with PHP code interleaaved with calls to insert form input field parts in the layout HTML.
Output the form using the function named Output.
|