Php Form Builder's Quick start Guide

Welcome to Php Form Builder's Quick start Guide

You'll find here basics to build any form, validate values and send them by email.

If you know how to deal with PHP objects you're at the right place.

If not, Beginners Guide will be more well-adapted.

For any question or request

Please

Requirements

All you need is a PHP server running with PHP 5.3+.

How it works

You need 4 PHP blocks in your page :

  1. 1st block at the very beginning of your page to :
    • create your form
    • validate if posted
    • send email (or record results in database, ...) if validated
  2. 2nd block just before </head> to include css files required by plugins
  3. 3rd between <body></body>to render your form
  4. 4th just before </body> to include js files & code required by plugins

Sample page code

<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
session_start();
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Form.php';
$form = new Form('test-form', 'horizontal', 'novalidate');
$form->addInput('text', 'user-name', '', 'Name :', 'required, placeholder=Name');
$form->addRadio('is-all-ok', 'Yes', 1);
$form->addRadio('is-all-ok', 'No', 0);
$form->printRadioGroup('is-all-ok', 'Is all ok ?', false, 'required');
$form->addPlugin('icheck', 'input', 'default', array('%theme%' => 'square', '%color%' => 'red'));
$form->addBtn('submit', 'submit-btn', 1, 'Send', 'class=btn btn-success');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Test Form</title>
    <!-- Latest compiled and minified Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <?php $form->printIncludes('css'); ?>
</head>
<body>
    <h1>My first form</h1>
    <?php
    if (isset($sent_message)) {
        echo $sent_message;
    }
    $form->render();
    ?>
    <!-- Latest compiled and minified jQuery -->
    <script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
    <!-- Latest compiled and minified Bootstrap JS -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <?php
    $form->printIncludes('js');
    $form->printJsCode();
?>
</body>
</html>

All functions and arguments to build your form, setup layout, add plugins are detailed in Class Documentation and Functions Reference.

Validate user's posted values and send email

Add this php block just after include_once [...] . '/phpformbuilder/Form.php'; :

if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('test-form') === true) {

    // create validator & auto-validate required fields
    $validator = Form::validate('test-form');

    // check for errors
    if ($validator->hasErrors()) {
        $_SESSION['errors']['test-form'] = $validator->getAllErrors();
    } else {
        $from_email = '[email protected]';
        $adress = '[email protected]';
        $subject = 'Test form email';
        $filter_values = 'submit-btn';
        $sent_message = Form::sendMail($from_email, $adress, $subject, $filter_values);
        Form::clear('test-form');
    }
}

Email sending may fail on your localhost, depending on your configuration.

It should work anyway on production server.

Complete page code

<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
session_start();
include_once rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR) . '/phpformbuilder/Form.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // create a new validator object & auto-validate required fields
    $validator = Form::validate('test-form');

    // check for errors
    if ($validator->hasErrors()) {
        $_SESSION['errors']['test-form'] = $validator->getAllErrors();
    } else {
        $from_email = '[email protected]';
        $adress = '[email protected]';
        $subject = 'Test form email';
        $filter_values = 'submit-btn';
        $sent_message = Form::sendMail($from_email, $adress, $subject, $filter_values);
        Form::clear('test-form');
    }
}
$form = new Form('test-form', 'horizontal', 'novalidate');
$form->addInput('text', 'user-name', '', 'Name :', 'required, placeholder=Name');
$form->addRadio('is-all-ok', 'Yes', 1);
$form->addRadio('is-all-ok', 'No', 0);
$form->printRadioGroup('is-all-ok', 'Is all ok ?', false, 'required');
$form->addPlugin('icheck', 'input', 'default', array('%theme%' => 'square', '%color%' => 'red'));
$form->addBtn('submit', 'submit-btn', 1, 'Send', 'class=btn btn-success');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Test Form</title>
    <!-- Latest compiled and minified Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <?php $form->printIncludes('css'); ?>
</head>
<body>
    <h1>My first form</h1>
    <?php
    if (isset($sent_message)) {
        echo $sent_message;
    }
    $form->render();
    ?>
    <!-- Latest compiled and minified jQuery -->
    <script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
    <!-- Latest compiled and minified Bootstrap JS -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <?php
    $form->printIncludes('js');
    $form->printJsCode();
?>
</body>
</html>

To go further

Now you've learned the basics ; Several resources will help to add other fields, plugins, validate different values and build more complex layouts :