Skip to content

simfatic/FormHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A simple PHP form handler

This package can be used to create simple PHP scripts that handles a form submission. At the moment, this PHP form handler validates, handles captcha, emails form submissions, and handles file attachments

Simple usage

use Simfatic\FormHandler\FormHandler;

$fh = new FormHandler(); 

$fh->validate(function($validator)
{
	$validator->field('name')->isRequired();
	$validator->field('email')->isEmail()->isRequired();

})->sendEmailTo('[email protected]');


echo $fh->process($_POST);

Form validations are done using Simfatic\Boar. So any of the customizations can go in the validate callback.

Customizing the mailer

The FormHandler uses PHPMailer. The options can be customized like this:

use Simfatic\FormHandler\FormHandler;

$fh = new FormHandler(); 

$fh->validate(function($validator)
{
	$validator->field('name')->isRequired();
	$validator->field('email')->isEmail()->isRequired();

})->configMailer(function($mailer)
{
	$mailer->setFrom('[email protected]','Form',false);
	
	$mailer->isSMTP();                                    // Send using SMTP
    $mailer->Host       = 'smtp.example.com';             // Set the SMTP server to send through
    $mailer->SMTPAuth   = true;                           // Enable SMTP authentication
    $mailer->Username   = '[email protected]';             // SMTP username
    $mailer->Password   = 'secret';                       // SMTP password
    $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
	$mailer->Port       = 587;   
	
	/*
	Example: adding Cc
	$mail->addCC('[email protected]');
	*/
	
})->sendEmailTo('[email protected]');


echo $fh->process($_POST);

File attachments

call attachFiles passing the name of the file upload fields.

$fh = new FormHandler(); 

$fh->validate(function($validator)
{
    $validator->field('name')->isRequired();
    $validator->field('email')->isEmail()->isRequired();

})->attachFiles(['image'])
->sendEmailTo('[email protected]');

ReCaptcha v2

In order to show the "I am not a robot" checkbox, create a div in your form like so:

<div class="g-recaptcha" data-sitekey="xxxxxxxxxxx"></div>

Then update the handler:

use Simfatic\FormHandler\FormHandler;

$pp = new FormHandler(); 

$pp->validate(function($validator)
{
	$validator->field('name')->isRequired();
	$validator->field('email')->isEmail()->isRequired();
})
->requireReCaptcha(function($recaptcha)
{
	$recaptcha->initSecretKey('xxxxxx');
})
->configMailer(function($mailer)
{
	$mailer->setFrom('[email protected]','Form',false);
})
->sendEmailTo('[email protected]');


echo $pp->process($_POST);

See a complete example in the examples folder

Client side handling

You can directly provide the link to your custom handler.php in the action attribute of the form.

Another alternative is to use Javascript to process the response from the form. The response from the process() function is JSON. Here is an example. This example uses jQuery.

$(function()
{
	$('#contact_form').submit(function(e)
	{
		e.preventDefault();
		$.post( 'handler.php', $('form#contact_form').serialize(), 
		    function(data) 
		      {
                if(data.result == 'success')
                {
                    $('form#contact_form').hide();
                    $('#success_message').show();
                }
                else
                {
                    $('#error_message').append('<ul></ul>')
                    jQuery.each(data.errors,function(key,val)
                    {
                        $('#error_message ul').append('<li>'+key+':'+val+'</li>');
                    });
                    $('#error_message').show();
                    
                }
		        
		      },'json');
	});
});

About

a simple form handler for common form tasks

Resources

License

Stars

Watchers

Forks

Packages

No packages published