Checks form fields and generates html-table to mail it to administrator
Makes similar checking of form fields filled on your web site.
It verifies required fields, including email addresses (checks @
existing and hostname MX record).
After that html report is generated and sent by mail(),
or user custom function, callback called with filtered fields and autosend is sent by same method., (*1)
Via Composer, (*2)
``` bash $ composer require romash1408/formchecker, (*3)
## Example ``` php require "vendor/autoload.php"; use R14\FormChecker; use R14\FormException; $usermail; $checker = new FormChecker( $forms = [ "mainform" => [ "subject" => "Test main form", "fields" => [ "name" => [ "placeholder" => "Name", ], "phone" => [ "type" => "tel", "placeholder" => "Phone number", "required" => true, ], "email" => [ "type" => "email", "placeholder" => "E-mail", "required" => true, ], "list" => [ "type" => "list", "placeholder" => "List", "required" => true, "list" => [ "One", "Two", "Three", ], ], ], "callback" => function($fields) use(&$usermail) { $usermail = $fields["email"]; // We can send post request with filtered and checked data to CRM server, if needed // $fields["key"] = "123456"; // Formsender::post("https://crm-server.com", $fields); }, "autosend" => [ "template" => function() { return <<<HTMLThank you for your request!
HTML; }, "address" => function() use(&$usermail) { return [ $usermail ]; }, "subject" => function() { return "Answer to user"; }, ], "to" => [ "admin@server" ], ], "contactform" => [ "subject" => "Contact form", "fields" => [ "author" => [ "type" => "email", "placeholder" => "Author e-mail", "required" => true, ], "message" => [ "type" => "textarea", "placeholder" => "Message", "required" => true, ], ], "to" => [ "moder@gmail.com" ], ], ], $config = [ "send" => function ($mail) { echo ' <h2>Sending mail to ' . implode(", ", $mail["to"]) . ' with subject "' . $mail["subject"] . '"</h2> ' . $mail["body"] . ' '; }, ] );
Now we have $checker
variable with information about all our forms and send function.
To make magic just call $checker->work()
with formname ("default"
by default) and
form fields array ($_POST
+ $_FILES
by default), like this:, (*4)
try { $checker->work($_POST["formname"]); } catch (FormException $e) { echo '<h2 style="color: red">' . $e->getMessage() . '</h2>'; }
``` php $_POST = [ "formname" => "mainform", ];, (*5)
#### Output: > ![Field Phone number must not be empty](https://romash1408.ru/untouchable/github-formchecker/readme/EMPTY_FIELD.jpg "Field Phone number must not be empty") ### #2 ``` php $_POST = [ "formname" => "mainform", "phone" => "+7 (999) 999-99-9", "email" => "test", ]; try { $checker->work($_POST["formname"]); } catch (FormException $e) { echo '<h2 style="color: red">' . $e->getMessage() . '</h2>'; }
, (*6)
``` php $_POST = [ "formname" => "mainform", "phone" => "+7 (999) 999-99-99", "email" => "test@gmail.co", ];, (*7)
try {, (*8)
$checker->work($_POST["formname"]);
} catch (FormException $e) {, (*9)
echo '<h2 style="color: red">' . $e->getMessage() . '</h2>';
}, (*10)
#### Output: > ![Incorrect email address format](https://romash1408.ru/untouchable/github-formchecker/readme/WRONG_EMAIL.jpg "Incorrect email address format") ### #4 ``` php $_POST = [ "formname" => "mainform", "name" => "Tester", "phone" => "+7 (999) 999-99-99", "email" => "test@gmail.com", ]; try { $checker->work($_POST["formname"]); } catch (FormException $e) { echo '<h2 style="color: red">' . $e->getMessage() . '</h2>'; }
, (*11)
``` php $_POST = [ "formname" => "mainform", "name" => "Tester", "phone" => "+7 (999) 999-99-99", "email" => "test@gmail.com", "list" => [ "One", "Four", ], ];, (*12)
try {, (*13)
$checker->work($_POST["formname"]);
} catch (FormException $e) {, (*14)
echo '<h2 style="color: red">' . $e->getMessage() . '</h2>';
}, (*15)
#### Output: > ![Did not found Four in List](https://romash1408.ru/untouchable/github-formchecker/readme/WRONG_LIST.jpg "Did not found Four in List") ### #6 ``` php $_POST = [ "formname" => "mainform", "name" => "Tester", "phone" => "+7 (999) 999-99-99", "email" => "test@gmail.com", "list" => [ "One", "Three", ], ]; try { $checker->work($_POST["formname"]); } catch (FormException $e) { echo '<h2 style="color: red">' . $e->getMessage() . '</h2>'; }
, (*16)
``` php
$_POST = [
"formname" => "contactform",
"author" => "work@romash1408.ru",
"message" => <<
try {, (*18)
$checker->work($_POST["formname"]);
} catch (FormException $e) {, (*19)
echo '<h2 style="color: red">' . $e->getMessage() . '</h2>';
} ```, (*20)
, (*21)