Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (2) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2021-06-22 (2 months ago) | Not enough user ratings | Total: 81 | All time: 9,723 This week: 200 |
Version | License | PHP version | Categories | |||
pformclass 1.0.0 | GNU General Publi... | 7 | HTML, PHP 5 |
Description | Author | |||||||||||||
This package can compose and display HTML forms on Web pages. |
|
Paul's Form Library is a PHP form library. You use it to make creating forms easier. It saves on writing the whole thing out in HTML.
The library allows you to create the following field types:
Normally, the individual fields are defined in an array. This is then
either fed to the form
class constructor, or fed to the form
object via
the set()
method. In the view, the individual fields are typically called
out where needed. For example:
$fields = [
'username' => [
'name' => 'username',
'type' => 'text',
'size' => 20,
'maxlength' => 20,
'required' => 1
],
'password' => [
'name' => 'password',
'type' => 'password',
'size' => 10,
'maxlength' => 20,
'required' => 1,
],
's1' => [
'name' => 's1',
'type' => 'submit',
'value' => 'Send'
]
];
$form = new form($fields);
The above code would be in the controller. In the view, there might be something like:
<form method="post" action="#">
<label>Username</label> <?php $form->text('username'); ?>
<br/>
<label>Password</label> <?php $form->password('password'); ?>
<br/>
<?php $form->submit('s1'); ?>
</form>
What follows are the various field types and the parameters which may be
defined for each field type. For the most part, the parameters for each
field correspond to attributes of the <input>
tag in HTML. For example,
every field has a "name" parameter. This corresponds to the "name"
attribute, like this:
<input name="name_of_field" ...>
Select fields (dropdown menus) require a series of options to work. You must define these before you define the field itself. These options are in an array, where each member contains two components-- a label component called "lbl", and a value component called "val". Like this:
$fruit_options = [
['lbl' => 'Apple', 'val' => 'A'],
['lbl' => 'Pear', 'val' => 'P'],
['lbl' => 'Watermelon', 'val' => 'W']
];
Other parameters:
Radio fields are like a hybrid of select fields and checkboxes. Like "select" fields, they require options to be defined before the field is specified. This is because, normally, you'd have more than one radio button which the user must select between.
An additional parameter is "direction". This comes about because you would normally have a label for each button. You want the buttons to appear on the right or the left of the label. In some cases, you want your radio buttons to stack one on top of each other. The direction parameter can take on these values:
The "options" parameter has the same structure as those of the "select" field type. But in the case of radio fields, the "lbl" parameter is the label which will appear next to the button. The "val" parameter is what registers when the particular button is pressed.
Parameters are as follows:
The parameters are as follows:
The parameters:
"Hidden" fields are specified in HTML and passed through the POST variable in forms.
The parameters:
The "file" field allows you to upload a field to a website. It gives the user a local file browsing window. The parameters are:
In order to make a "file" field work, there are two additional
requirements. First you must add an "enctype" clause to the <form>
tag
like this:
<form method="post" action="#" enctype="multipart/form-data">
The second thing needed is a hidden field like this:
<input type="hidden" name="MAX_FILE_SIZE" value="100000000"/>
You can specify this this way:
$fields = [
'MAX_FILE_SIZE' => [
'name' => 'MAX_FILE_SIZE',
'type' => 'hidden',
'value' => 1000000000
],
...
];
These are the buttons on a form. Parameters as follows:
There are two static methods which can be used in the library. The first
is the button()
method. Here is the function signature:
static function button($legend, $link)
This creates a clickable button with a label on it. The first parameter is the label for the button. The second parameter is the URL you want the user to go to when the button is pressed.
The second method is this:
static function abandon($link)
This is like the "button()" method, except that you don't specify the label; it says Abandon. The parameter you pass is the link the user goes to when he or she presses the button.
There are two ways to instantiate or populate the form object. The first way to do it is to pass the fields array to the constructor, like this:
$form = new form($fields)
The second way to populate the form object with fields is to use the
set()
method. This assumes you've already instantiated a form object.
Like this:
$form = new form();
$form->set($fields);
There are three ways of outputting fields to your form screen. As shown above, you can use the method corresponding to your field type, like this:
<?php $form->text('name'); ?>
There is a secondary way to call this function, where you feed the method any default value for that field. Like this:
<?php $form->text('name', 'Paul Foster'); ?>
You can accomplish this same thing by using the "value" parameter in specifying your field, as above.
It's worth noting that you can't use the wrong method for your field. That
is, you can't use the text()
routine to display a "password" field.
You'll get an error.
Another way to display a field is to use the output()
method, like this:
<?php $form->output('name', 'Paul Foster'); ?>
In this case, you may use the output()
method with any field type. It's a
sort of a generic method. The only problem with it is that when you're
looking over your view code, and you've used this method, you won't know
the type of the fields involved.
The third way to display your fields is in aggregate. In other words, you make one call for the whole form, rather than one function call for each field. Like this:
<?php $form->show(); ?>
In order to make this work properly, you must surround it with a table, like this:
<form method="post" action="#">
<table>
<?php $form->show(); ?>
</table>
</form>
Normally, you would manually specify a label for each field, entirely
outside of the form method calls. The output()
method allows you to avoid
this. The output()
method will take the "name" parameter of the field
definition to act as the label for ths field. This is true unless you also
specify a "label" parameter on a field definition. In that case, this
"label" string will be used instead.
There should be a file called formtest.php
accompanying this package.
This is an actual example of how this code is put together. You can examine
this code and copy or modify it as you like.
This code is licensed under GPLv2. If you distribute it or an altered copy of it, you're obligated to distribute the source code as well.
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
0% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.