|
PHP Resources -
Handy PHP Functions
|
|
Written by M. L. Griswold
|
|
Saturday, 16 September 2006 |
|
|
Description:
|
|
|
string build_input_field(string type, string name[, string value[, int size[, int maxlength[, string option[, string prepend[, string append]]]]]])
|
|
|
|
|
|
|
This handy PHP function will reduce the amount of typing you do when you are designing HTML forms. While all you need to enter is the input type and name for the function to work, it offers the flexability to build very complex input tags if needed. The "option" parameter allows for extra arguments to be inserted inside of the input tag while the "prepend" and "append" parameters allow for additional output to be added before and after the tag.
|
|
|
|
|
|
Function:
|
|
|
function build_input_field($type, $name, $value = '', $size = '', $maxlength = '', $option = '', $prepend = '', $append = '') {
// Function written by Marcus L. Griswold (vujsa)
// Can be found at http://www.handyphp.com
// Do not remove this header!
$field = "<input type=\"$type\" name=\"$name\"";
if($value) $field .= " value=\"$value\"";
if($size) $field .= " size=\"$size\"";
if($maxlength) $field .= " maxlength=\"$maxlength\"";
if($option) $field .= " $option";
$field .= ">";
$field = $prepend.$field.$append;
return $field;
}
|
|
|
|
|
|
Usage:
|
|
|
echo build_input_field('text', 'sample', 'Sample Text', 20, '', 'title="Insert Some Text Here!"', "\tTest Field: ", "<br>\n");
|
|
|
|
|
|
Result:
|
|
|
Test Field: <input type="text" name="sample" value="Sample Text" size="20" title="Insert Some Text Here!"><br>
|
|
|
|
|
|
Notes:
|
|
|
This is only really practical if you are planning to add a lot of input fields manually. Using a loop to build a form would be a good substitute for this kind of manual form creation. This will of course work for text, hidden, password, button, reset, and submit input fields.
|
Discuss this article on the forums. (1 posts)
|