|
Registered User
Join Date: Mar 2003
Location: Boise, Idaho
Posts: 4
|
Off Topic but makes PHP Design Easier
I have begun writing a (IMO) useful PHP script that I like to call 'Fast Forms.' As the name implies it is a script that you can include in your PHP scripts for quick generation and validation of HTML Input Forms.
It isn't quite complete and I am going to write a component that will allow for verification checks on fields, IE a US Zip code field contains 5 numbers, email address looks like an e-mail address, phone numbers are only numeric, etc...
If there is enough interest I can post the updates that I will be making over the next several weeks.
The comments at the tail end of the script contain examples on usage. You can look at the specific functions for variable examples.
Feel free to modify but I would appreciate the credit and/or improvements to be mailed back.
DISCLAIMER: The code has not been "beautified", yet.
PHP Code:
<?
/*******************************
fastForms v0.50
--- by Nathan Cook <ncook@pcsedu.com>
*******************************/
/*******************************
• FileField Function Needs Flesh
• FileField Behind the scenes needs
planned and written
A set of functions to rapidly
create, validate and provide
user feedback for HTML forms.
*******************************/
if(!$font) { $font = "<font face=verdana,arial,helvetica size=2>"; }
$PHP_SELF = $_SERVER[PHP_SELF];
// Initializes the form, creates global variables to track required vars, track errors,
// report errors and begins the buffer to draw the table. $$formname, form print buffer
function startForm($title, $name, $bgcolor, $postto, $target, $width, $cellpadding, $cellspacing, $border, $encType = 0)
{
global $form, $formName, $font;
if($encType == '1') { $encPrint = "enctype=\"multipart/form-data\""; } else { unset($encPrint); }
if($border != 0) {
$form .= "<table cellpadding=$border cellspacing=0 border=0 bgcolor=#000000><tr><td>\n";
}
$formName = $name;
if($target != "") { $targetPrint = "target=$target"; }
$form .= "<table bgcolor=$bgcolor border=0 width=$width cellpadding=$cellpadding cellspacing=$cellspacing>\n";
// create <form> with $postto, $targetPrint
$form .= "<form $encPrint method=POST action=".$postto." $targetPrint>\n";
// Draw header for table proclaiming the $title
$form .= "<tr>\n\t<td colspan=2>$font <font size=+1>";
if($title != "") $form .= "<b>$title</b><br>";
$form .= "<hr size=1 width=99%></td>\n</tr>\n";
}
function reqd($required, $name, $label) {
global $form, $formName, $font, $errorArr;
hidField('required[]', $name);
if($errorArr != "") {
if(array_search($name, $errorArr)) {
$label = '<font color=#FF0000>'.$label.'</font>';
}
}
$label = '<b>'.$label.'</b>';
return $label;
}
function heading($title, $bold)
{
global $form, $formName, $font;
if($bold == '1') { $text = "<b>$title:</b>"; } else { $text = $title; }
$form .= "<tr>\n\t<td colspan=2 height=22>$font $text</td>\n</tr>\n";
}
// Draws a text field based on the variables supplied.
function textField($label, $name, $size, $maxlength, $required = '0', $disabled = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label </td>\n";
// Print Field in second cell
$form .= "\t<td>$font <input type=text name=$name value=\"".$_REQUEST[$name]."\" size=$size maxlength=$maxlength $dis></td>\n";
$form .= "</tr>\n";
}
function dateInput($label, $name, $required = '0', $disabled = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label </td>\n";
$nameMon = $name."Mon";
$nameDay = $name."Day";
$nameYear = $name."Year";
// Print Field in second cell
$form .= "\t<td>$font <input type=text name=".$name."[] value=\"".$_REQUEST[$name][0]."\" size=2 maxlength=2 $dis> / \n";
$form .= "\t$font <input type=text name=".$name."[] value=\"".$_REQUEST[$name][1]."\" size=2 maxlength=2 $dis> / \n";
$form .= "\t$font <input type=text name=".$name."[] value=\"".$_REQUEST[$name][2]."\" size=4 maxlength=4 $dis></td>\n";
$form .= "</tr>\n";
}
function phoneField($label, $name, $required = '0', $disabled = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label </td>\n";
// Print Field in second cell
$form .= "\t<td>$font (<input type=text name=".$name."[] value=\"".$_REQUEST[$name][0]."\" size=3 maxlength=3 $dis>) \n";
$form .= "\t$font <input type=text name=".$name."[] value=\"".$_REQUEST[$name][1]."\" size=3 maxlength=3 $dis> - \n";
$form .= "\t$font <input type=text name=".$name."[] value=\"".$_REQUEST[$name][2]."\" size=4 maxlength=4 $dis></td>\n";
$form .= "</tr>\n";
}
function passField($label, $name, $size, $maxlength, $required = '0', $disabled = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label </td>\n";
// Print Field in second cell
$form .= "\t<td>$font <input type=password name=$name value=\"".$_REQUEST[$name]."\" size=$size maxlength=$maxlength $dis></td>\n";
$form .= "</tr>\n";
}
// Draws a checkbox field based on the variables supplied
function checkField($label, $name, $value)
{
global $form, $formName, $font;
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font - $label</td>\n";
// Print Field in second cell
if(isset($_REQUEST[$name])) { $checked = "checked"; } else { unset($checked); }
$form .= "\t<td>$font <input type=checkbox name=$name value=\"$value\" $checked></td>\n";
$form .= "</tr>\n";
}
// Draws a hidden field based on the variables supplied
function hidField($name, $value)
{
global $form, $formName, $font;
// print field for hidden value and name
$form .= "\t<input type=hidden name=\"$name\" value=\"$value\">\n";
}
// Draws a file upload field based on the variables supplied
function fileField($label, $name, $filesize, $required = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label</td>\n";
// Print Field in second cell
$form .= "\t<td>$font <input type=file name=$name></td>\n";
$form .= "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$filesize\">";
$form .= "</tr>\n";
}
// Begins a set of radio fields based on the variables supplied
function radioField($label, $name, $none = '0', $required = '0', $disabled = '0')
{
global $form, $formName, $font, $nonoption;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $label</td>\n";
// begin cell for radio buttons
if($none == '1') {
$form .= "\t<td>$font <input type=radio name=$name value=\"0\" $dis>None</td>\n";
$form .= "</tr>\n";
}
$nonoption = $none;
}
// Adds options to the radio fields based on the variables supplied
function radioOption($label, $name, $value)
{
global $form, $formName, $font, $nonoption;
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
if($nonoption == '1') {
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font </td>\n";
}
// begin cell for radio buttons
if($_REQUEST[$name] == $value) { $selected = "checked\n"; } else { unset($selected); }
$form .= "\t<td>$font <input type=radio name=$name value=\"$value\" $selected $dis>$label</td>\n";
$form .= "</tr>\n";
$nonoption = '1';
}
// Adds options to the select field based on the variables supplied
function selectOption($label, $value, $name)
{
global $form, $formName, $font, $$name;
// Detect pre-selected fields
// Creates/Appends to a string the options with default values
if($_REQUEST[$name] == $value) { $selected = "selected\n"; } else { unset($selected); }
$$name .= "\t\t\t<option value=\"$value\" $selected>".$label."</option>\n";
}
// Initializes a select field based on the variables supplied
// YOU MUST DEFINE ALL SELECT OPTIONS BEFORE DEFINING FIELDS
function selectField($title, $name, $size = '1', $required = '0', $disabled = '0')
{
global $form, $formName, $font, $$name;
if($required == '1') { $title = reqd($required, $name, "$title"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// create row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75%>$font $title</td>\n";
// second cell: <select $name $size>
$form .= "\t<td>";
$form .= "$font\n\t\t <select name=$name size=$size $dis>\n";
$form .= $$name;
$form .= "\n\t\t</select>\n";
$form .= "\t</td>\n";
$form .= "</tr>\n";
}
// Draws a text area based on the variables supplied
function areaField($label, $name, $rows, $cols, $wrapstyle, $required = '0', $disabled = '0')
{
global $form, $formName, $font;
if($required == '1') { $label = reqd($required, $name, "$label"); }
if($disabled == '1') { $dis = 'disabled'; } else { unset($dis); }
// Draw Table Row
$form .= "<tr>\n";
// Print $label in first cell
$form .= "\t<td width=75% valign=top>$font $label</td>\n";
// Print Field in second cell
$form .= "\t<td>$font <TEXTAREA name=$name cols=$cols rows=$rows wrap=$wrapstyle $dis>".$_REQUEST[$name]."</textarea></td>\n";
$form .= "</tr>\n";
}
// Ends Form, adds hidden fields for passing the global arrays.
function endForm($button, $border)
{
global $form, $formName, $font;
// print submit button
$form .= "<tr>\n\t<td colspan=2 align=right>\n\t\t<hr size=1 width=99%>\n\t\t$font \n\t\t<input type=submit name=submit value=\"$button\">\n\t</td>\n</tr>\n";
// close table
$form .= "</table>\n";
//if($border != 0) {
$form .= "</td></tr></table>\n";
//}
}
// parses through the global arrays to validate form fields
function validateForm()
{
global $form, $formName, $font, $errorArr, $errorStr, $valid;
if(count($_REQUEST[required])) {
foreach($_REQUEST[required] as $field) {
if(($_REQUEST[$field] == "")||($_REQUEST[$field] == '0')) {
$errorArr .= ",$field";
if(!$errorStr) { $errorStr = "Complete the Bold Red fields to continue."; }
}
}
}
if($errorArr != "") {
$errorArr = explode(",", $errorArr);
} else {
$valid = '1';
}
}
function errorOutput($errorStr)
{
global $form, $formName, $font;
if($errorStr != "") {
$form .= "<tr>\n\t<td colspan=2 align=center>$font <font color=#FF0000><b>$errorStr</b></font></font></td>\n</tr>\n";
}
}
/* Example ****************************
if($_REQUEST[submit]) { validateForm(); }
$formTitle = 'Testing';
$formName = 'test';
$bgcolor = '#DDDDDD';
$postto = $PHP_SELF;
$target = '';
$width = '350';
$cellpadding = '2';
$cellspacing = '0';
$border = '1';
startForm($formTitle, $formName, $bgcolor, $postto, $target, $width, $cellpadding, $cellspacing, $border);
errorOutput($errorStr);
textField('First Name', 'fname', '20', '40', 1);
textField('Last Name', 'lname', '20', '40', 1);
heading('Your Organization', 0);
checkField('School', 'school', '1');
checkField('YMCA', 'ymca', '1');
checkField('Boys & Girls Club', 'bgca', '1');
radioField('Sex', 'sex', '0', '1');
radioOption('Male', 'sex', 'male');
radioOption('Female', 'sex', 'female');
selectOption('<- Select One ->', '0', 'jobtitle');
selectOption('Programmer', 'prog', 'jobtitle');
selectOption('SysAdmin', 'sysad', 'jobtitle');
selectOption('Developer', 'develop', 'jobtitle');
selectField('Job Title', 'jobtitle', '1', '1');
areaField('Notes', 'notes', '4', '20', 'virtual', '1');
endForm('Submit', 1);
print($form);
********************************** */
?>
|