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 have some knowledge of PHP coding you're at the right place.

Not very comfortable coding in PHP?

A complete step-by-step tutorial is available here: PHP Beginners Guide

Requirements

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

Installation

PHP Form Builder's package includes the Form Builder itself, the documentation and all the templates.

You don't have to upload all the files and folders on your production server.

Documentation and Templates are available online at https://www.phpformbuilder.pro/.
There's no need to upload them on your production server.


  1. Add phpformbuilder folder at the root of your project.

    Your directory structure should be similar to this :

    Minimum required files

    • mailer folder if you send emails
    • Validator folder (for server-side validation)
    • Form.php
    • register.php - delete it once registered

    If you use any plugin, add its folder & xml file, for example phpformbuilder/plugins/select2 and phpformbuilder/plugins-config/select2.xml


    More details about folders, files and required files on production server here: ../index.html#package-structure

  2. Registration

    Open phpformbuilder/register.php in your browser, enter your purchase code to activate your copy.

    Each purchased license allows to install PHP Form Builder on 2 different domains:

    • 1 install for localhost
    • 1 install for your production server

    Once you activated your purchase, remove register.php from your production server to avoid any unwanted access.

  3. You're ready to go.

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

/*============================================
=            1st block - the form            =
============================================*/

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->addBtn('submit', 'submit-btn', 1, 'Send', 'class=btn btn-success');

// iCheck plugin
$form->addPlugin('icheck', 'input', 'default', array('%theme%' => 'square', '%color%' => 'red'));

/*=====  End of 1st block  ======*/

?>
<!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

/*============================================================
=            2nd block - css includes for plugins            =
============================================================*/

$form->printIncludes('css');

/*=====  End of 2nd block  ======*/

?>
</head>
<body>
<h1>My first form</h1>
<?php

/*======================================================================================================
=            3rd block - render the form and the feedback message if the form has been sent            =
======================================================================================================*/

if (isset($sent_message)) {
echo $sent_message;
}
$form->render();

/*=====  End of 3rd block  ======*/

?>
<!-- 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

/*========================================================================
=            4th block - js includes for plugins and js code (domready)            =
========================================================================*/

$form->printIncludes('js');
$form->printJsCode();

/*=====  End of 4th block ======*/

?>
</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 {
$email_config = array(
'sender_email'    => '[email protected]',
'sender_name'     => 'PHP Form Builder',
'recipient_email' => '[email protected]',
'subject'         => 'PHP Form Builder - Test form email',
'filter_values'   => 'test-form'
);
$sent_message = Form::sendMail($email_config);
Form::clear('test-form');
}
}

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

It should work anyway on production server.

Database recording

Jeff L. Williams's Mysql class is in database folder.

You'll find all documentation and insert / edit / delete examples here : class-doc.php#database-main

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" && 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 {
$email_config = array(
'sender_email'    => '[email protected]',
'sender_name'     => 'PHP Form Builder',
'recipient_email' => '[email protected]',
'subject'         => 'PHP Form Builder - Test form email',
'filter_values'   => 'test-form'
);
$sent_message = Form::sendMail($email_config);
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 :