7/23/2010: I have been working on a helper class for building forms with PHP. I have seen several of these built inside of frameworks but I have been unable to find a stand-alone implementation. I have built a basic helper class but I think it is too bulky. I am going to work on refactoring the INPUT, TEXTAREA, and SELECT portions of the code. I also have to add validation so ensure emails and invalid characters are not entered. I have included some of the source files. You can offer improvements or use the code yourself as a starting point. I will continue to make this into a better version.
The Helper Class
This is the class used for the generating the form. The display_form function takes a few variables to define the form and an array to populate the form fields. The initial function takes the array of form elements and accesses the private functions, input and textarea, depending on the string passed into the display_form function. I will soon be adding functionality for the select form element as well.
<?php
class Form {
public function display_form($form_name, $action, $method, $form_fields=array()){
$output = "<fieldset>".PHP_EOL;
$output .= "<legend>".$form_name."</legend>".PHP_EOL;
$output .= "<form name='".$form_name."' action='".$action."' method='".$method."'>";
$output .= "<ul>";
$all_fields = "";
foreach($form_fields as $field_array => $fields){
if($fields['0']=="input"){
$all_fields .= self::input($fields);
}elseif($fields['0']=="textarea"){
$all_fields .= self::textarea($fields);
}elseif($fields['0']=="select"){
}else{
$all_fields .= "<p><strong>".$fields['0']."</strong> is not a valid form element.</p>";
}
}
$output .= $all_fields;
$output .= "</ul>";
$output .= "</form></fieldset>";
echo $output;
}
// $element, $type="", $name="", $value="",$options=array()
private static function input($fields=array()){
$output = "";
$output .= "<li>".PHP_EOL;
$output .= " <label for='".$fields['2']."'>".ucwords(str_replace('_', ' ', ($fields['1']!="radio" ? $fields['2'] : $fields['3']) ))."</label>".PHP_EOL;
$output .= " <".$fields['0']." type='".$fields['1']."' name='".$fields['2']."' value='".$fields['3']."' ";
if(!empty($fields['4'])){
foreach($fields['4'] as $option => $string){
$output .= $option."='".$string."' ";
}
}
$output .= "/>".PHP_EOL." </li>".PHP_EOL;
if(isset($fields['4']['startfieldset'])){
$output = "<fieldset><legend>".$fields['4']['startfieldset']."</legend>".$output;
}elseif(isset($fields['4']['endfieldset'])){
$output = $output."</fieldset>";
}
return $output;
}
// $element, $name, $cols="20", $rows="5", $value="",$options=array()
private static function textarea($fields=array()){
$output = "<li>".PHP_EOL;
$output .= " <label for='".$fields['1']."'>".ucwords(str_replace('_', ' ', $fields['1']))."</label>".PHP_EOL;
$output .= " <textarea name='".$fields['1']."' cols='".$fields['2']."' rows='".$fields['3']."' ";
if(!empty($fields['5'])){
foreach($fields['5'] as $option => $string){
$output .= $option."='".$string."' ";
}
}
$output .= ">".$fields['4']."</textarea>".PHP_EOL." </li>".PHP_EOL;
return $output;
}
}
$form = new Form;
?>
Implementing the Form Helper Class
To utilize this code simply include the php class file and build your form. This looks a whole lot nicer than a messy HTML form. You can see exactly what is going into this form at a glance. This code will generate a form like the one I have at the top.
<?php
$form_fields[] = array("input", "text", "full_name", "", array("class"=>"your_style", "id"=>"your_id"));
$form_fields[] = array("input", "text", "email", "", array());
$form_fields[] = array("input", "radio", "version", "1.0", array("class"=>"nowidth", "startfieldset"=>"Versions"));
$form_fields[] = array("input", "radio", "version", "1.1", array("class"=>"nowidth", "endfieldset"=>"Versions"));
$form_fields[] = array("input", "checkbox", "super_checkbox", "checked", array("class"=>"nowidth"));
$form_fields[] = array("textarea", "message", "20", "5", "", array());
$form_fields[] = array("input", "submit", " ", "Send", array("class"=>"submit"));
$form->display_form('Contact', '', 'POST', $form_fields);
?>
7-12-2010: I have been studying PHP and MySQL a lot lately. I have built several PHP applications and developed a full featured CMS. I thought It might be a good time to become certified.
I chose W3 Schools because of their concentration on standards compliance. Building webpages is not about just getting an end product. It is about building something that
someone will not have trouble with but will enjoy their visit. I spend countless hours doing error checking. They encourage that and provide an excellent training program.
I got my certification in PHP. Click the picture to view the online certificate.
7-1-2010: I have found a great javascript plugin for limiting the type of characters that are used in a text field. It is extremely easy to use and setup.
The files are small and it makes a must have for someone looking to limit the types of characters in an input or textarea field. The plugin also restricts the ability to copy
and paste characters into the field. The plugin uses the jquery framework to run. You can read the full details here.
6-30-2010: I have been so busy updating other people's websites that I have been unable to continuously update mine. God has provided a lot of work for me this summer from places I never would have expected. As work continues to flow, I have been able to learn PHP and MySQL. From these I have started building ecommerce websites and custom content manages systems. I think my next thing to tackle might be a framework, or I might build my own!
4-21-09 Here is the first post of the new year! It has been a great semester. There have been some rough points but I must say that I am pleased with how well it has turned out. (that may change as exams come up in a few weeks) I just finished my last paper of the semester and my documentary is almost done. I have grow tremendously through some of my Christian studies classes. I never did understand the richness of the scriptures until this year. Next semester I am taking 20 hours and the classes are going to be difficult. The Arts Education project for this year is almost completed and I am ready to start working for some real money this summer. I am looking foward to being home again but sad about the slow internet. I get so spoiled at school.
There are many things that I have picked up this semester. I have done more work in After Effects and Photoshop than I ever have before. I am increasing my working speed in all the programs that I have learned and work has been a lot less stressfull toward the end of this semester. Tendergrassfarm.com is almost complete. I have been working on that for about 5 months.
Melissa and I have been having a lot of fun together. We just got back from Myrtle Beach. Her parents let me come for free because I helped do some work on their land. We have been dating now for a year and 5 months! Only more excitement and revelations to come!
12-23-08 It has been almost a year since my last post and much has happened. Right now Christmas is only a few days away and everybody is excited. What are they excited about? How many of us pick up the Christmas tree and shop for endless hours for the perfect gift understand why? Why is this holiday is so important? It represents the beginning of our salvation. Without the birth of the one who came to rescue our lifeless souls we would still be doomed.
I haven't been to Pennsylvania for quite sometime now. This year we are taking Melissa with us. She is very excited to meet the rest of the family. I am excited that she is coming too and happy to get away from all the work that I have been doing. This year I quite soccer. It was a very difficult decision but one that needed to be made. I struggled this year to find time to relax. Soccer became more of a job than a game. I will still play on my club team but professionally my soccer career is over. I have played for 18 years and every year it got more time consuming until this year it completely dictated my life.
1-18-08 I guess I lied about updating this site more often but I have been very busy with other projects that required my attention. I really don't have time right now considering the 9 hours of video I took in Guatemala the last couple of weeks. I have done several other projects recently like the arts education DVD for my school, Christmas Cantata for my church, Herbs777.com for my dad, a few business card designs for my dad as well, an instructional video to maintain uniformity in our news station, a Constitution Day Video, and a Soccer Promotional Video for my team.
Life is not all about work but has a great deal to do with the people you surround yourself with and get to know more closely. God has blessed me with the unthinkable, a beautiful young lady. I am not an eloquent writer so if you ever wish to know the story behind this miracle you will have to ask me personally. If you wish to test how close we really are I challenge you to try to take one of us from the other. You will soon find out what she and I both know.
8-05-07 Back at school again. Now I will be able to update this more often. A lot has happened this summer but nothing that I regret. I worked most of the time and when I wasn't working I was with my friends. My brother Randy came to visit and that was exciting. Soccer starts Tuesday and I am soo out of shape. We will see how everything goes...
6-08-07 Well I know it has been a while since this was updated but I have been really busy. All my friends are graduating and I am busy hanging out with them. I have been working at the warehouse for about 2 weeks now and it could be worse but its not pleasant. We have almost tripled our workers in the past year because of so much work that is being done. I am back home now and it is sooo nice to be with all of my friends again. I have missed them so much. Ray's website is finally done and you can check it out at www.flyforhim.org. Looking ahead to the rest of the summer just seams to be more work but I might be able to go on the teen mission trip to do their video.
5-16-07 We are back from Orlando and I have begun to work on the website again. If Tammy and Ray get me the information for the rest of the website it should be done by the end of this week. I miss being at home but it is still fun here. My passport expired so I will not be able to fly to Haiti with Ray. I am kinda disappointed about that but what can you do.
5-10-07 Wow time flies when you are in Florida. It only seems like yesterday and I was just finishing my 11 hour drive. After almost a week of being here I understand why Tammy and Ray rarely come to visit us. I have had a lot of fun staying here and have been diligently working on their website and videos. You can look at Ray's current website at www.flyforhim.org. I will update again when I finish his new one. Tomorrow we are off to Orlando for a wonderful weekend.
4-29-07 Most of my exams are finished. I only have one more tomorrow and it won't be very difficult. I am going to miss school but It will be nice to have a break. For out last video project of the year we are shooting the graduation. I am going miss a lot of the people graduating and some of the people that are leaving. We'll see how things change next year.
4-24-07 School here is coming to a close. This weekend we will have exams. This summer I am I am going to go to Florida to work with my brother Ray. I will be working on Ray's mission video and his website. I am planning only to be there until the end of May. Soccer ended well this semester. We were in Spring Training but we had 3 games that we won. This summer will hopefully bring in some new income as well.