Quick web forms in php

One of the most boring task for me during development is that of creating forms. I like to get the task done as quickly as possible and get on with the back-end code. There are many javascript and php libraries that enable you to do just that, but you always need to integrate the two together to get a workable form. MVC frameworks provide a nice way to quickly create a form with all the validations and errors nicely built in. But for non-framework applications we have to rely on libraries or create a form code from scratch.

A few days back I stumbled on jFormer, a nice PHP form processor,that lets you create pleasant forms with validations and other nice features without much of a hassle. The library relies on jQuery for client side validation and processing.

A quick code

A sample code to display a ‘contact’ form with validation and form processing is shown below. Although this might look a bit complex, remember that all the JavaScript validation and display is done automatically by the code.

<?php
 
require_once('jformer.php');
 
$contactForm = new JFormer('contactForm', array());
 
$contactForm->addJFormComponentArray(array(
    new JFormComponentName('name', 'Name:', array(
        'validationOptions' => array('required'),
    )),
    new JFormComponentSingleLineText('email', 'E-mail address:', array(
        'width' => 'long',
        'validationOptions' => array('required', 'email'), // notice the validation options
    )),
    new JFormComponentSingleLineText('subject', 'Subject:', array(
        'width' => 'longest',
        'validationOptions' => array('required'),
    )),
    new JFormComponentTextArea('message', 'Message:', array(
        'width' => 'longest',
        'height' => 'medium',
        'validationOptions' => array('required'),
    )),
));
 
// Set the function for a successful form submission
function onSubmit($formValues) {
 
    if(!empty($formValues->name->middleInitial)) {
        $name = $formValues->name->firstName.' '.$formValues->name->middleInitial.' '.$formValues->name->lastName;
    }
    else {
        $name = $formValues->name->firstName.' '.$formValues->name->lastName;
    }
 
    // Send the message - you would need to include a mailing package or some other way to send your message
    // the return array returns to jformer and tells it how to how to handle the response
    if(!$mail->Send()) {
        $return = array('status' => 'failure', 'response' => $mail->ErrorInfo);
        $return['failureNoticeHtml'] = 'There was a problem sending your e-mail.'; // failureNoticeHtml returns and html error message
    }
    else {
        $return = array('status' => 'success', 'response' => 'Message successfully sent.');
        $return['successPageHtml'] = '<h1 style="margin-bottom: 0.5em;"><cufon style="width: 67px; height: 20.8px;" alt="Thanks " class="cufon cufon-canvas"><canvas style="width: 82px; height: 21px; top: 0px; left: -1px;" height="21" width="82"></canvas><cufontext>Thanks </cufontext></cufon><cufon style="width: 29px; height: 20.8px;" alt="for " class="cufon cufon-canvas"><canvas style="width: 44px; height: 21px; top: 0px; left: -1px;" height="21" width="44"></canvas><cufontext>for </cufontext></cufon><cufon style="width: 102px; height: 20.8px;" alt="Contacting " class="cufon cufon-canvas"><canvas style="width: 117px; height: 21px; top: 0px; left: -1px;" height="21" width="117"></canvas><cufontext>Contacting </cufontext></cufon><cufon style="width: 22px; height: 20.8px;" alt="Us" class="cufon cufon-canvas"><canvas style="width: 33px; height: 21px; top: 0px; left: -1px;" height="21" width="33"></canvas><cufontext>Us</cufontext></cufon></h1><p>Your message has been successfully sent. We will respond as soon as possible.</p>'; // successPageHtml returns html for a success page. this can be any html.
    }
 
    return $return;
}
 
    // Process any request to the form
$contactForm->processRequest(false);
?>

There are some other nice demos on the site that you can play with, I specially like the ‘survey’ form demo. One major irritation though is the lack of documentation, which I hope the authors quickly take care of.

4 Responses

  1. Rodrigo says:

    You can customize error messages?

  2. Hey mate. My name is Justin and I just stumbled on your site for the first time. I’m interested in your blog, well your articles, because I myself am an Ardruino developer, web developer, etc. Looks like we have some of the same hobbies.

    Keep it up man!

  3. sameer says:

    Hello Rodrigo!

    You can customize the error messages. More documentation will be coming in a weeks time from the authors.

  4. JohnD says:

    Hi,
    Nice article .
    I have never used Jquery , so sory for my dump question :)
    Where can I download the “jformer.php” ?
    ” require_once(‘jformer.php’); ”

    Thanks