Makes creating and managing html forms easier than answering the phone and writing it down.
| Form | ( [ $vars ] ) |
| required | ( $required ) |
| values | ( [ $values ] ) |
| check | ( $check ) |
| process | ( $process ) |
| upload | ( $upload [, $filesize ] ) |
| validate | ( $formname [, $function ] ) |
| error | ( [ $name [, $message ]] ) |
| header | ( $formname [, $method [, $action ]] ) |
| field | ( $type, $name [, $prompt [, $options [,$key ]]] ) Radio Buttons and Check Boxes ( 'radio', $name, $prompt='', $values=array() ) ( 'checkbox', $name, $prompt='', $value='' ) ( 'multicheck', $name, $prompt='', $value='', $key='' ) Select Form Fields ( 'select', $name, $prompt='', $values=array() ) ( 'multiselect', $name, $prompt='', $values=array() ) ( 'hierselect', $name, $prompt='', $values=array() ) Text Fields ( 'text', $name, $prompt='', $options=array('width'=>250, 'maxlength'=>50) ) ( 'multitext', $name, $prompt='', $options=array('width'=>250, 'maxlength'=>50, 'value'=>''), $key='' ) ( 'password', $name, $prompt='', $options=array('width'=>100, 'maxlength'=>20) ) ( 'file', $name, $prompt='', $options=array('size'=>50) ) ( 'textarea', $name, $prompt='', $options=array('width'=>400, 'height'=>200) ) ( 'calendar', $name, $prompt='' ) Form Fields ( 'hidden', $name, $value ) ( 'submit', $name ) |
| recaptcha | ( ) |
| close | ( ) |
| An Advanced PHP for Beginners Example | |
| The (X)HTML Output | |
| Final Result | |
| object Form ( [ string $vars ] ) | top |
Create a new form object.
<?php
$form = new Form ('class=awesome|autocomplete=off');
?>
| required ( array $required ) | top |
You can only require 'calendar', 'file', 'select', 'hierselect', 'multiselect', 'password', 'text', and 'textarea' form fields.
<?php
$form->required(array('color', 'condition', 'category', 'subcategory', 'email', 'password', 'verify', 'comments', 'date'));
?>
| array values ( [ array $values ] ) | top |
This method is to preselect the values of form inputs. Form 'multicheck' and 'multitext' inputs cannot be required or values preselected - except for 'multitext' by specifying 'value'=>'preselected' in the options array of each $form->field().
<?php
$form->values(array('gender'=>'M',
'bike'=>'Y',
'airplane'=>'Y',
'color'=>'y',
'condition'=>array(1, 3, 4),
'category'=>2,
'subcategory'=>5,
'email'=>'your@email.com',
'comments'=>'This is a brilliant class, man!',
'date'=>'02/07/2002'
));
?>
| check ( array $check ) | top |
These are the form inputs you want to check and make sure that they have the kind of values you expect of them. To return all of the values of a 'multicheck' form type use $form->process() below, and for 'file' form types use the $form->upload() method.
<?php
$form->check(array('gender'=>array('M', 'F'),
'bike'=>'YN',
'car'=>'YN',
'airplane'=>'YN',
'add'=>'YN', // as this is a multicheck it will not return unselected values
'color'=>array('r', 'b', 'w', 'y'),
'condition'=>'int',
'category'=>'int',
'subcategory'=>'int',
'email'=>'email',
'amount'=>'float',
'password'=>'pass',
'verify'=>'password',
'comments'=>'',
'date'=>'date',
'secret'=>''
));
?>
| process ( array $process ) | top |
To process[] multicheck and multitext input. We have this method because of the browsers bad habit of not returning multicheck values that are unselected. It is not necessary, but if you have multitext inputs with all of the same array keys, you might as well include them here with the multichecks. Don't forget to include a $form->field('hidden', 'process', $key) with each multicheck.
<?php
$form->process(array('add'=>'YN'));
?>
| upload ( array $upload [, integer $filesize= 5 ] ) | top |
This method will make sure the file(s) are of the correct type, and if so upload them to your uploads folder. If that is successful it will add to the $vars array returned by the $form->validate() function the files name, or if unsuccessful will trigger an error to alert you of what is happening, and if it is part of the $form->required() array will inform the user they need to try and resubmit the form again.
<?php
$form->upload(array('image'=>array('jpg', 'gif', 'png')), 2);
?>
| array validate ( string $formname [, string $function= 'escape_data' ] ) | top |
This is where you collect all of the user-submitted information, process it, then send them on their merry way.
<?php
list($vars, $errors, $eject) = $form->validate('formname');
if (!empty($vars) && empty($errors)) {
$html .= '<pre>' . print_r($vars, true) . '</pre>';
// $page->eject ($eject, 'Thanks!<br />Your form submission was successful.');
}
?>
| array error ( [ string $name [, string $message ]] ) | top |
Unfortunately I was unable to include every validation routine known to coders. This method is so that you can do your own sanitization, make a custom error, and have your user try again.
<?php
if ($vars['name'] != 'what I like') {
$form->error('name', 'Sorry Charlie, try again.');
}
if (empty($form->error()) { // Continue . . .
?>
| string header ( string $formname [, string $method= 'post' [, string $action]] ) | top |
This is what gets the party started. Everything until now was just getting set up for the action.
<?php
$html .= $form->header('formname');
?>
| string field ( string $type, string $name [, string $prompt [, string or array $options [, integer $key ]]] ) | top |
This method creates the actual form fields. You tell it the $type of field, what you want to $name it, $prompt the user so they know what to input, and give them (including the input field) their $options. If the $name is part of the $form->required array and the user tries to submit the form without filling it in properly, the $prompt turns red and the user is reminded of the fact - whether they have javascript enabled or not.
<?php
#-- Radio Buttons and Check Boxes --#
$html .= $form->field('radio', 'gender', 'Gender:', array('M'=>'Male', 'F'=>'Female'));
$html .= $form->field('checkbox', 'bike', 'I have a bike.', 'Y');
$html .= $form->field('checkbox', 'car', 'I have a car.', 'Y');
$html .= $form->field('checkbox', 'airplane', 'I have an airplane.', 'Y');
$html .= '<h3><u>Add:</u></h3>';
$html .= $form->field('multicheck', 'add', 'Four', 'Y', 4) . $form->field ('hidden', 'process', 4);
$html .= $form->field('multicheck', 'add', 'Eight', 'Y', 8) . $form->field ('hidden', 'process', 8);
$html .= $form->field('multicheck', 'add', 'Fifteen', 'Y', 15) . $form->field ('hidden', 'process', 15);
#-- Select Form Fields --#
$html .= $form->field('select', 'color', 'Personality:', array('r'=>'Red', 'b'=>'Blue', 'w'=>'White', 'y'=>'Yellow')) . '<br />';
$html .= $form->field('multiselect', 'condition', 'Condition:', array(1=>'Alive', 2=>'Curious', 3=>'Crazy', 4=>'Gifted', 5=>'Puzzled')) . '<br />';
#-- Our Multi-Dimensional Array for the Hierselect Category --#
$categories['hier'] = 'subcategory';
$categories[0] = array(1=>'Planes', 2=>'Trains', 3=>'Automobiles');
$categories[1] = array(1=>'Cessna 172', 2=>'Learjet', 3=>'Boeing 747');
$categories[2] = array(4=>'Amtrak', 5=>'The Alaska Railroad', 6=>'Bullet Train');
$categories[3] = array(7=>'Volkswagen Beetle', 8=>'Hummer', 9=>'Bugatti Veyron');
$values = $form->values();
if (isset($values['category'])) { // This is to preload the subcategories if a category has already been selected
$selected = $values['category'];
$subcategories = $categories[$selected];
} else {
$subcategories = array();
}
$html .= $form->field('hierselect', 'category', 'Category:', $categories)) . '<br />';
$html .= $form->field('select', 'subcategory', 'Subcategory:', $subcategories)) . '<br />';
#-- Text Fields --#
$html .= $form->field('text', 'email', 'Email:', array('maxlength'=>60)) . '<br />';
$html .= $form->field('multitext', 'amount', 'Item 16:', array('width'=>50, 'maxlength'=>10), 16) . '<br />';
$html .= $form->field('multitext', 'amount', 'Item 23:', array('width'=>50, 'maxlength'=>10), 23) . '<br />';
$html .= $form->field('multitext', 'amount', 'Item 42:', array('width'=>50, 'maxlength'=>10, 'value'=>108), 42) . '<br />';
$html .= $form->field('password', 'password', 'Password:') . '<br />';
$html .= $form->field('password', 'verify', 'Verify:') . '<br />';
$html .= $form->field('file', 'image', 'Image:') . '<br />';
$html .= $form->field('textarea', 'comments', 'Comments:<br />') . '<br />';
$html .= $form->field('calendar', 'date', 'Date:') . '<br />';
#-- Form Fields --#
$html .= $form->field ('hidden', 'secret', 'value');
$html .= $form->field('submit', 'Submit');
?>
| string recaptcha ( ) | top |
Automatic and easy integration with reCAPTCHA. Check it out at their site recaptcha.net. All you need to do is sign up, put your public and private key in the script you uploaded from us, and make sure you include() it at the top of your page.
<?php
$html .= $form->recaptcha();
?>
| string close ( ) | top |
Closes up shop, and finishes your form's code.
<?php
$html .= $form->close();
?>
| An Advanced PHP for Beginners Example: | top |
<?php
$page = new Page;
$page->link('style.css');
// $page->doctype ('html', 'transitional');
require_once (BASE_URI . 'include/Advanced_PHP/recaptcha.php');
$html = '<div style="margin:10px;">';
$form = new Form ('class=awesome|autocomplete=off');
$form->required(array('color', 'condition', 'category', 'subcategory', 'email', 'password', 'verify', 'comments', 'date'));
$form->values(array('gender'=>'M', 'bike'=>'Y', 'airplane'=>'Y', 'color'=>'y', 'condition'=>array(1, 3, 4), 'category'=>2, 'subcategory'=>5, 'email'=>'your@email.com', 'comments'=>'This is a brilliant class, man!', 'date'=>'02/07/2002'));
$form->check(array('gender'=>array('M', 'F'), 'bike'=>'YN', 'car'=>'YN', 'airplane'=>'YN', 'color'=>array('r', 'b', 'w', 'y'), 'condition'=>'int', 'category'=>'int', 'subcategory'=>'int', 'email'=>'email', 'amount'=>'float', 'password'=>'pass', 'verify'=>'password', 'comments'=>'', 'date'=>'date', 'secret'=>''));
$form->process(array('add'=>'YN'));
$form->upload(array('image'=>array('jpg', 'gif', 'png')));
list($vars, $errors, $eject) = $form->validate('formname');
if (!empty($vars) && empty($errors)) {
$html .= '<pre>' . print_r($vars, true) . '</pre>';
if (!empty($vars['image'])) unlink (BASE_URI . 'uploads/' . $vars['image']);
// $page->eject ($eject, 'Thank you!<br /><br />Your form submission was successful.<br /><br />Sorry, we deleted your file.');
}
$html .= $form->header('formname');
#-- Radio Buttons and Check Boxes --#
$html .= $form->field('radio', 'gender', 'Gender:', array('M'=>'Male', 'F'=>'Female')) . "<hr />\n ";
$html .= $form->field('checkbox', 'bike', 'I have a bike.', 'Y') . "<hr />\n ";
$html .= $form->field('checkbox', 'car', 'I have a car.', 'Y') . "<hr />\n ";
$html .= $form->field('checkbox', 'airplane', 'I have an airplane.', 'Y') . "<hr />\n ";
$html .= 'Include:' . $form->field('multicheck', 'add', 'Four', 'Y', 4) . $form->field ('hidden', 'process', 4);
$html .= $form->field('multicheck', 'add', 'Eight', 'Y', 8) . $form->field ('hidden', 'process', 8);
$html .= $form->field('multicheck', 'add', 'Fifteen', 'Y', 15) . $form->field ('hidden', 'process', 15) . "<hr />\n ";
#-- Select Form Fields --#
$html .= $form->field('select', 'color', 'Personality:', array('r'=>'Red', 'b'=>'Blue', 'w'=>'White', 'y'=>'Yellow')) . "<hr />\n ";
$html .= $form->field('multiselect', 'condition', 'Condition:', array(1=>'Alive', 2=>'Curious', 3=>'Crazy', 4=>'Gifted', 5=>'Puzzled')) . "<hr />\n ";
#-- Our Multi-Dimensional Array for the Heirselect Category --#
$categories['hier'] = 'subcategory';
$categories[0] = array(1=>'Planes', '2'=>'Trains', '3'=>'Automobiles');
$categories[1] = array(1=>'Cessna 172', 2=>'Learjet', 3=>'Boeing 747');
$categories[2] = array(4=>'Amtrak', 5=>'The Alaska Railroad', 6=>'Bullet Train');
$categories[3] = array(7=>'Volkswagen Beetle', 8=>'Hummer', 9=>'Bugatti Veyron');
$values = $form->values();
if (isset($values['category'])) { // This is to preload the subcategories if a category has already been selected
$selected = $values['category'];
$subcategories = $categories[$selected];
} else {
$subcategories = array();
}
$html .= $form->field('hierselect', 'category', 'Category:', $categories) . "<hr />\n ";
$html .= $form->field('select', 'subcategory', 'Subcategory:', $subcategories) . "<hr />\n ";
#-- Text Fields --#
$html .= $form->field('text', 'email', 'Email:', array('maxlength'=>60)) . "<hr />\n ";
$html .= $form->field('multitext', 'amount', 'Item 16:', array('width'=>50, 'maxlength'=>10), 16) . "<hr />\n ";
$html .= $form->field('multitext', 'amount', 'Item 23:', array('width'=>50, 'maxlength'=>10), 23) . "<hr />\n ";
$html .= $form->field('multitext', 'amount', 'Item 42:', array('width'=>50, 'maxlength'=>10, 'value'=>108), 42) . "<hr />\n ";
$html .= $form->field('password', 'password', 'Password:') . "<hr />\n ";
$html .= $form->field('password', 'verify', 'Verify:') . "<hr />\n ";
$html .= $form->field('file', 'image', 'Image:') . "<hr />\n ";
$html .= $form->field('textarea', 'comments', 'Comments:<br />') . "<hr />\n ";
$html .= $form->field('calendar', 'date', 'Date:') . "<hr />\n ";
#-- reCAPTCHA Field --#
$html .= $form->recaptcha() . "<hr />\n ";
#-- Form Fields --#
$html .= $form->field ('hidden', 'secret', 'value') . "\n ";
$html .= $form->field('submit', 'Submit') . "\n ";
$html .= $form->close();
unset ($form);
$html .= '</div>';
echo $page->display ($html);
?>
| The (X)HTML Output: | top |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Advanced PHP for Beginners</title>
<meta name="description" content="Advanced PHP for Beginners">
<meta name="keywords" content="Advanced PHP for Beginners">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="http://www.advancedphpforbeginners.com/combine/style-form_fields.css">
<script type="text/javascript" src="http://www.advancedphpforbeginners.com/combine/form_calendar-form_hierselect-form_validation.js"></script>
</head>
<body>
<div style="margin:10px;"><form method="post" action="http://www.advancedphpforbeginners.com/code/sample/form.php?submitted=formname" onsubmit="return lookOver(this);" enctype="multipart/form-data" class="awesome" autocomplete="off"><fieldset style="border:0px; padding:0px;"><legend style="display:none;"> </legend><input type="hidden" name="MAX_FILE_SIZE" value="5120000"><input type="hidden" name="formname" value="true">Gender:<input type="radio" style="border-width:0px;" name="gender" id="ap_731bf5d6d7" value="M" checked="checked"><label for="ap_731bf5d6d7">Male</label><input type="radio" style="border-width:0px;" name="gender" id="ap_a121cae91f" value="F"><label for="ap_a121cae91f">Female</label><hr>
<input type="checkbox" style="border-width:0px;" name="bike" id="ap_8f69f5f69c" value="Y" checked="checked"><label for="ap_8f69f5f69c">I have a bike.</label><hr>
<input type="checkbox" style="border-width:0px;" name="car" id="ap_33fb80f7ab" value="Y"><label for="ap_33fb80f7ab">I have a car.</label><hr>
<input type="checkbox" style="border-width:0px;" name="airplane" id="ap_703c9ea665" value="Y" checked="checked"><label for="ap_703c9ea665">I have an airplane.</label><hr>
Include:<input type="checkbox" style="border-width:0px;" name="add[4]" id="ap_a940c59bfc" value="Y"><label for="ap_a940c59bfc">Four</label><input type="hidden" name="process[]" value="4"><input type="checkbox" style="border-width:0px;" name="add[8]" id="ap_4622aa38fa" value="Y"><label for="ap_4622aa38fa">Eight</label><input type="hidden" name="process[]" value="8"><input type="checkbox" style="border-width:0px;" name="add[15]" id="ap_8320fa0267" value="Y"><label for="ap_8320fa0267">Fifteen</label><input type="hidden" name="process[]" value="15"><hr>
<label for="color">Personality:<select name="color" id="color" class="reqd"><option value=""> </option><option value="r">Red</option><option value="b">Blue</option><option value="w">White</option><option value="y" selected="selected">Yellow</option></select></label><hr>
<label for="condition">Condition:<select name="condition[]" id="condition" class="reqd int" multiple="multiple" size="5"><option value="1" selected="selected">Alive</option><option value="2">Curious</option><option value="3" selected="selected">Crazy</option><option value="4" selected="selected">Gifted</option><option value="5">Puzzled</option></select></label><hr>
<label for="category"><script type="text/javascript"> var hiersubcategory = '1:1:Cessna 172|1:2:Learjet|1:3:Boeing 747|2:4:Amtrak|2:5:The Alaska Railroad|2:6:Bullet Train|3:7:Volkswagen Beetle|3:8:Hummer|3:9:Bugatti Veyron|'; </script>Category:<select name="category" id="category" class="reqd int" onchange="hierSelect(hiersubcategory, 'subcategory', this.options[this.selectedIndex].value)"><option value=""> </option><option value="1">Planes</option><option value="2" selected="selected">Trains</option><option value="3">Automobiles</option></select></label><hr>
<label for="subcategory">Subcategory:<select name="subcategory" id="subcategory" class="reqd int"><option value=""> </option><option value="4">Amtrak</option><option value="5" selected="selected">The Alaska Railroad</option><option value="6">Bullet Train</option></select></label><hr>
<label for="email">Email:<input type="text" name="email" id="email" class="reqd email" value="your@email.com" style="width:200px;" maxlength="60"></label><hr>
<label for="ap_46245ba8f5">Item 16:<input type="text" id="ap_46245ba8f5" name="amount[16]" style="width:50px;" maxlength="10" value=""></label><hr>
<label for="ap_b208f7979a">Item 23:<input type="text" id="ap_b208f7979a" name="amount[23]" style="width:50px;" maxlength="10" value=""></label><hr>
<label for="ap_cdaba991fb">Item 42:<input type="text" id="ap_cdaba991fb" name="amount[42]" style="width:50px;" maxlength="10" value="108"></label><hr>
<label for="password">Password:<input type="password" name="password" id="password" class="reqd psword" maxlength="20" style="width:100px;"></label><hr>
<label for="verify">Verify:<input type="password" name="verify" id="verify" class="reqd password" maxlength="20" style="width:100px;"></label><hr>
<label for="image">Image:<input type="file" size="50" name="image" id="image"></label><hr>
<label for="comments">Comments:<br><textarea name="comments" id="comments" class="reqd" cols="60" rows="7" style="width:400px;height:200px;">This is a brilliant class, man!</textarea></label><hr>
<label for="date">Date:<input type="text" name="date" id="date" class="reqd date" style="width:80px;" maxlength="10" value="02/07/2002"><span id="calendardate" style="font-family:Tahoma,Sans-Serif; font-size:11px; position:absolute; visibility:visible; margin:5px; z-index:100;"></span><img src="/include/head/calendar.jpg" alt="calendar" onclick="javascript:formCalendar('date', 'calendardate');"></label><hr>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LccZQYAAAAAANt69keuELNjqav8bJmTB4syLzpi"></script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=6LccZQYAAAAAANt69keuELNjqav8bJmTB4syLzpi" height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript><hr>
<input type="hidden" name="secret" value="value">
<input type="submit" name="submit" class="submit" value="Submit">
</fieldset></form></div>
</body>
</html>| Final Result: | top |