Formr is a PHP micro framework which allows developers to easily build forms; but what if you’re not a PHP developer? What if you don’t even know PHP? No problem, Formr has you covered!
Formr isn’t at all complicated; to install it all you have to do is download Formr from GitHub, unzip it and drag the
Formr folder into your site’s root folder and type:
and that’s it! Formr is now installed and ready to go to work.
Building a Form
Build your form in HTML like you normally would, and when you come to your form element’s
value attribute you’ll enter a small snippet of PHP that will allow Formr to do the heavy lifting for you.
Your form will now look something like this:
So now if your visitor submits the form and there’s an error, the information will be displayed in the field so they won’t have to retype and fill out the whole form again.
Processing the Form
Once your visitor clicks the
submit button you need to process the posted information and do something with it, which usually involves typing lots of code and validating the information and sending an email or putting something in a database. This is really easy with Formr.
First thing we do is check if the form has been submitted, we do this with the
Next, we process and validate our form fields. The easiest way to do this is to use Formr’s
fastpost() function, and let it do all the work for us. We’ll take all of the form fields and put them into an array and name it
This will process every field in your form and put it into that
$post array for later use. Granted, it’s not incredibly flexible and won’t perform strict validation on every field, but it will clean all input (making it safe) and will validate email addresses, among a few other things.
So now our script looks like this:
Checking for Errors
If you’ve made some form fields required - and the visitor didn’t fill them out - Formr will let them know via error messages, but first you must tell Formr which fields are required and where to display the error messages.
To make fields required we use the
required variable, like so:
Entering an asterisk
* tells Formr that you want all fields required. If you only want some of the fields required just enter a comma separated list of those field’s names.
To display error messages, we use the
This can appear anywhere in your document, as along as it’s after where you check if the form has been submitted, i.e., after
Now that our form has been processed, let’s check for errors, and if no errors are found we’ll email the results to our client. Since we’re doing a simple email let’s skip putting our
POST values into the
$post array and just send them right from inside Formr’s
send_email() email function!