I was recently searching for a form template that uses Twitter’s Bootstrap framework. PFBC, the PHP Form Builder Class, was recently refactored with Bootstrap, and it also supports validation and reCAPTCHA. PFBC does not include a form handler, but it does take the difficult and redundant work out of creating self-validating forms.
Adding an auto-mailer to PFBC is very simple, however. You just need a simple PHP if/else and all is well.
If you have read this far, you should go ahead and download PFBC version 3 (with Bootstrap). You’ll want to install it on a dev server and read the documentation, which is on the example pages. Installation: put the downloaded folder in a public_html space that’s running PHP 5+.
Note that there are different packages for PHP 5.3, so get the correct flavor of version 3. This example works with PHP 5.3.
I have created an example form that includes most of the different types of inputs, all you need do is create a file in the same folder as the package’s main index.php. I’ve heavily commented the code, so you should be able to easily understand it.
<?php // php-form-builder-class PFBC // set up name spaces for form, elements, and validation use PFBC\Form; use PFBC\Element; use PFBC\Validation; session_start(); error_reporting(E_ALL); include("PFBC/Form.php"); include("header.php"); $version = file_get_contents("version"); // has the form been submitted and passed validation? // If so, send email and display a confirmation message. if(Form::isValid($_POST["form"])) { /*The form's submitted data has been validated. Your script can now proceed with any further processing required.*/ //get the POST data from the form $Course = $_POST['Course']; $Semester = $_POST['Semester']; $WhyUnavailable = $_POST['WhyUnavailable']; $DetailUnavailable = $_POST['DetailUnavailable']; $Classification = $_POST['Classification']; $Major = $_POST['Major']; $Email = $_POST['Email']; $Comments = $_POST['Comments']; // loop through the When field's checkbox group and output them all with a return between values foreach ($_POST['When'] as $val) { $When .= $val."\n"; } $DetailWhen = $_POST['DetailWhen']; // create and send the email $to = "you@yourdomain.com"; $email_from = $Email; $email_subject = "Course Request: $Course, $Semester, $When"; $email_body = "Course: $Course\n\n". "Semester: $Semester\n\n". "Why: $WhyUnavailable\n$DetailUnavailable\n\n". "Classification: $Classification\n". "Major: $Major\n\n". "Email: $Email\n\n". "Comments: $Comments\n\n". "When offered:\n$When". "$DetailWhen\n\n"; $headers = "From: $email_from \r\n". "Reply-To: $email_from \r\n"; mail($to,$email_subject,$email_body,$headers); echo " <div class=\"alert alert-success\"> <a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a> <strong class=\"alert-heading\"> Thank you for submitting a course Course Request. By providing this feedback you will help us to make adjustments to the schedule both now and in the future.</strong> </div>"; }//end if(Form::isValid // is the form loaded for the first time, or does input need to be entered or corrected? // if so, display the form. Validation prompts are displayed by the form itself. else { header("Location: " . $_SERVER["PHP_SELF"]); /*Validation errors have been found. We now need to redirect back to the script where the form exists so the errors can be corrected and the form re-submitted.*/ echo "<h1>What course do you need?</h1> <p>We are committed to doing all we can to help you graduate in as timely a fashion as possible. We know that sometimes, as you arrange your schedule for the upcoming semester, you are unable to find an available seat in a course that you need. Whenever possible we aim to be responsive to students' needs. By providing this feedback you will help us to make adjustments to the schedule both now and in the future.</p> <div class=\"alert alert-warning\"> <a class=\"close\" data-dismiss=\"alert\" href=\"#\">×</a> <strong class=\"alert-heading\">* denotes required fields.</strong> </div>"; // The form identifier $form = new Form("CourseNeed"); // Prevent bootstrap and jquery from re-loading, already loaded in doc head $form->configure(array( "prevent" => array("bootstrap", "jQuery") )); // Validation field for feedback messages to user $form->addElement(new Element\Hidden("form", "CourseNeed")); // Form fields // simple textbox, required $form->addElement(new Element\Textbox("Course:", "Course", array( "required" => 1, "shortDesc" => "Department and course number of class you need", //"placeholder" => "MATH 101" ))); // simple select, required $form->addElement(new Element\Select("Semester:", "Semester", array( // first array is the options "",// blank value to force a choice "Fall", "Spring", "Summer I", "Summer II", ), array( // second array is the parameters "Required" => 1, // required to force a choice "shortDesc" => "Choose One…" ))); // simple select, required $form->addElement(new Element\Select("Why Unavailable:", "WhyUnavailable", array( // first array is the options "",// blank value to force a choice "All the scheduled spots are full", "All available classes conflict with my schedule", "The course is not being offered in that semester", "Other", ), array( // second array is the parameters "Required" => 1, // required to force a choice "shortDesc" => "Choose One… Why is the course unavailable to you?" ))); $form->addElement(new Element\Textbox("Details:", "DetailUnavailable", array( "shortDesc" => "If 'Other': Why is the course unavailable to you?" ))); // simple select, required $form->addElement(new Element\Select("Your Classification:", "Classification", array( // first array is the options "",// blank value to force a choice "Freshman", "Sophomore", "Junior", "Senior", "Graduate", ), array( // second array is the parameters "Required" => 1, // required to force a choice "shortDesc" => "Choose Your Classsification…" ))); $form->addElement(new Element\Textbox("Major:", "Major", array( "required" => 1, "shortDesc" => "Your Major" ))); // email, self-validating, required $form->addElement(new Element\Email("Email:", "Email", array( "Required" => 1, "shortDesc" => "Preferably, a university email address", "placeholder" => "you@domain.com" ))); // simple textarea $form->addElement(new Element\Textarea("Comments:", "Comments", array( "placeholder" => "Optional: additional information that may explain your situation or your needs." ))); // simple checkbox set $form->addElement(new Element\Checkbox("When:", "When", array( // first array is the options "Morning", "Afternoon", "Evening", "Online", "Other", ), array( // second array is the parameters "Required" => 1, // required to force a choice "shortDesc" => "Please choose time(s) when would you like the course offered…" ))); $form->addElement(new Element\Textbox("Details:", "DetailWhen", array( "shortDesc" => "If 'Other': When would you like the course offered?" ))); // reCAPTCHA with validation, automaticallly required $form->addElement(new Element\Captcha("Validation:", array( "longDesc" => "This is a <a href=\"http://en.wikipedia.org/wiki/ReCAPTCHA\"> reCaptcha test</a> to reduce spam submissions." ))); // submit button and cancel button $form->addElement(new Element\Button); $form->addElement(new Element\Button("Cancel", "button", array( "onclick" => "history.go(-1);" ))); // draw the form $form->render(); }//end else
If you want to implement a form on a site that does not include Twitter Bootstrap, you may find the bootstrap.css conflicts with many of your site’s styling.
In that case, you can go to Bootstrap’s Customize and Download page, and choose only the css, js and components you need:
Scaffolding
none
Base CSS
Forms, Buttons
Components
Button groups, Alerts
JS Components
Collapse
Miscellaneous
Close icon
Utilities
Component animations
Responsive
none
I couldn’t get this to work as written. The browser report tahat a redirect can’t be completed. Basicaly it was stuck in a loop reloading the page. By moving the IsValid method to the end of the form so the form loads first I could get it to work.
What version of PHP are you running? This example works with PHP 5.3+