PHPackages                             justcoded/form-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. justcoded/form-handler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

justcoded/form-handler
======================

1.1(7y ago)1713010[3 issues](https://github.com/justcoded/form-handler/issues)MITPHP

Since Feb 2Pushed 7y ago2 watchersCompare

[ Source](https://github.com/justcoded/form-handler)[ Packagist](https://packagist.org/packages/justcoded/form-handler)[ RSS](/packages/justcoded-form-handler/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

Static forms FormHandler library
================================

[](#static-forms-formhandler-library)

Small library to validate simple html forms data and send requests to email. Furthermore you can write your own "handler" to process valid data, for example if you need to save it through API to a 3d-party service like Mailchimp, SalesForce, CRM system, etc.).

Why FormHandler
===============

[](#why-formhandler)

It's very easy to find some ready-to-use solution to process a contact form. Usually this is pure PHP script, which collect data and send email with php `mail()` function. It's not bad, but you can find numerous problems with such scripts:

- `mail()` function can be blocked on production server, because it's not secure. Also it's often goes to SPAM folder, when you use `mail()` function.
- You need to validate, that the data is valid. Manual validation of the `$_POST` array is time consuming and require knowledge of PHP, RegExp's, etc.

We decide to create small library, which fix all these issues, so to process a form you need:

- set validation rules with simple configuration array
- set your Mail settings (SMTP settings OR Mandrill API key)
- set your message params (From, To, Subject, Body template)

And that's it!

Requirements
============

[](#requirements)

- PHP 7.0+
- [Composer](http://getcomposer.org/download)
- Working SMTP server to send emails, or Mandrill account with configured mail domain.

Usage
=====

[](#usage)

Imagine you have simple html website with a contact form and you want to process it. We have `name`, `email`, and `message` form fields.
We will guide you through the whole process of creating PHP script to process a form request.

1. Init your environment
------------------------

[](#1-init-your-environment)

We suggest to create separate folder to place code into it. Let's call it `form`. File structure will looks like this:

```
|- /form/        # folder for our code
|- contact.php   # simple HTML page with a form

```

Inside `/form/` folder we need to create `composer.json` file to set our library requirement:

```
{
    "require": {
        "justcoded/form-handler": "*"
    }
}

```

Now we need to download all required files with a composer, by running a bash command:

```
composer install

```

2. Entry file
-------------

[](#2-entry-file)

You must create entry file, which will handle the form request. You can copy one of our examples `examples/basic.php` or `examples/advanced.php` inside package folder.

Let's call our file `form.php` and create it from scratch. It should be accessible from browser (for example: `http://MY-DOMAIN.COM/form/form.php`).

After that you need to include composer autoloader script and then set use part for library classes:

```
