Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ttskch/contact-form

PHP utility classes to implement general contact form
https://github.com/ttskch/contact-form

contact-form email form sendmail

Last synced: 16 days ago
JSON representation

PHP utility classes to implement general contact form

Awesome Lists containing this project

README

        

# contact-form

[![Travis (.com) branch](https://img.shields.io/travis/com/ttskch/contact-form/master.svg?style=flat-square)](https://travis-ci.com/ttskch/contact-form)
[![Latest Stable Version](https://poser.pugx.org/ttskch/contact-form/v/stable?format=flat-square)](https://packagist.org/packages/ttskch/contact-form)
[![Total Downloads](https://poser.pugx.org/ttskch/contact-form/downloads?format=flat-square)](https://packagist.org/packages/ttskch/contact-form)

PHP utility classes to implement general contact form (also with confirmation view). It's maybe useful to build contact form on WordPress or pure PHP site.

## Requirements

* PHP 5.4+
* Configuring `date.timezone` in php.ini

## Supported features

* Csrf protection with session and hidden input tag
* Server side validation for submitted values
* **Short hands to print submitted value itself, validation errors, "selected" option, "checked" option**
* Attaching files and handle them easily in the same session
* Sending email which contains submissions information easily

## Installation

```bash
$ composer require ttskch/contact-form
```

or

```bash
$ git clone [email protected]:ttskch/contact-form.git
$ cd contact-form
$ composer install --no-dev

# If your web site is not composer-friendly, upload whole "contact-form" directory by hand.
```

## Usage

```php
validateAndRedirectAfterSelfPosted('./confirm.php', $requiredKeys, $emailKeys);
?>



= $cf->csrfHiddenInput(); ?>




= $cf->presentError('Name'); ?>






= $cf->presentError('Email'); ?>





presentSelected('Gender', 'Male', $default = true); ?>>Male
presentSelected('Gender', 'Female'); ?>>Female
presentSelected('Gender', 'Other'); ?>>Other

Confirm

```

```php
rejectAccessWithoutSubmissions('./index.php');

// after posted, validate csrf and redirect to next page
$cf->validateAndRedirectAfterSelfPosted('./thanks.php');
?>


= $cf->csrfHiddenInput(); ?>


= $cf->present('Name'); ?>


= $cf->present('Email'); ?>


= $cf->present('Gender'); ?>

Send


Back

```

```php
rejectAccessWithoutSubmissions('./index.php');

$template = <<present('Name', false),
$cf->present('Email', false),
$cf->present('Gender', false),
]);

$cf->sendEmail(
'[email protected]', // to
'[email protected]', // from
'Your Name', // from name
'Got inquiry', // subject
$body // body
);

// clear submissions after sending email
// by this, if users reload thanks.php after sending email they will be redirected to index.php
$cf->clearSubmissions();
?>

Form is successfully submitted!


```

See [demo](demo) code or run it on your local to learn more :)

### Precaution

Because this utility calls [header()](https://www.php.net/manual/en/function.header.php) function to redirect, you must execute instantiation and some methods before any actual output is sent.