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.

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'] = '

Thanks for Contacting Us

Your message has been successfully sent. We will respond as soon as possible.

'; // 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 thoughts to “Quick web forms in php”

  1. 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!

  2. 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

Comments are closed.