Skip to content

Formr for Designers

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!

Installation

The first step is to download Formr from GitHub, unzip it and drag the Formr folder into your site's folder, then copy the following code and paste it into your PHP form script.

<?php
    require 'Formr/class.formr.php';
    $form = new Formr\Formr();
?>

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 with Formr's value() function.

$form->value('the_fieldname_goes_here');

Your form will now look something like this:

<input type="text" name="name" value="<?php $form->value('name'); ?>">
<input type="email" name="email" value="<?php $form->value('email'); ?>">

When 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 form information and do something with it, which usually involves typing lots of code to validate the information, plus 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 with the submitted() function.

if ($form->submitted())
{
    // the form has been submitted
}

Next, we process and validate our form fields. An easy 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 $post.

$post = $form->fastpost('POST');

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:

if ($form->submitted())
{
    $post = $form->fastpost('POST');

    $name = $post['name'];
    $email = $post['email'];
}

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. Just enter a comma separated list of the required field names. If you want to make all fields required, just enter an asterisk (*) instead.

$form->required = 'name,email';

To display error messages, we use the messages() function.

$form->messages()

This can appear anywhere in your layout, as along as it's after where you check if the form has been submitted.

Sending Email

Our form has been processed and no errors are found; now what? Let's email the results to our client!

Since we're doing a simple email, let's iust skip that $post array we created earlier and just send them right from inside Formr's send_email() function!

<?php
if ($form->submitted())
{
    if (!$form->errors())
    {
        $to = 'client@email.com';
        $subject = 'Form Submission';
        $from = 'donotreply@domain.com';

        $form->send_email($to, $subject, 'POST', $from);
    }
}
?>

Our Final File

<!DOCTYPE html>
<html lang="en">
<body>
<?php
    require 'Formr/class.formr.php';
    $form = new Formr\Formr();
    $form->required = '*';
    if ($form->submitted()) {
        if (!$form->errors()) {
            $to = 'client@email.com';
            $subject = 'Form Submission';
            $from = 'donotreply@domain.com';

            if ($form->send_email($to, $subject, 'POST', $from)) {
                $form->success_message = 'Email Sent!';
            }
        }
    }
    $form->messages(); 
?>
    <form action="form.php" method="post">
        <input type="text" name="name" value="<?php $form->value('name'); ?>"><br>
        <input type="email" name="email" value="<?php $form->value('email'); ?>"><br>
        <button type="submit" name="submit">Submit Form</button>
    </form>
</body>
</html>

Comments