Skip to content

Commit 0edcfcb

Browse files
committed
Some readme adjustments
1 parent c758f85 commit 0edcfcb

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

README.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Small library to validate simple html forms data and send requests to email.
66
Furthermore you can write your own "handler" to process valid data, for example if you need to save
77
it through API to a 3d-party service like Mailchimp, SalesForce, CRM system, etc.).
88

9-
## Why FormHandler
9+
# Why FormHandler
1010

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

1515
* `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.
16-
* 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 knowledge etc.
16+
* 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.
1717

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

@@ -23,23 +23,19 @@ We decide to create small library, which fix all these issues, so to process a f
2323

2424
And that's it!
2525

26-
## Requirements
26+
# Requirements
2727

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

31-
## Usage
32+
# Usage
3233

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

37-
### Examples
38-
39-
You can check working examples inside `examples` folder of the package, start your investigate from `index.php` file (contains forms HTML).
40-
There you can find which files loaded next, when you submit forms.
41-
42-
### Init your environment
38+
## 1. Init your environment
4339

4440
We suggest to create separate folder to place code into it. Let's call it `form`.
4541
File structure will looks like this:
@@ -51,15 +47,15 @@ Inside `/form/` folder we need to create `composer.json` file to set our library
5147

5248
{
5349
"require": {
54-
"justcoded/form-handler": "^1.0.*"
50+
"justcoded/form-handler": "*"
5551
}
5652
}
5753

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

6056
composer install
6157

62-
### 3. Entry file
58+
## 2. Entry file
6359

6460
You must create entry file, which will handle the form request.
6561
You can copy one of our examples `examples/basic.php` or `examples/advanced.php` inside package folder.
@@ -80,7 +76,7 @@ use JustCoded\FormHandler\DataObjects\MailMessage;
8076
use JustCoded\FormHandler\FileManager\FileManager;
8177
```
8278

83-
### 4. Form processing
79+
## 3. Form processing
8480

8581
Form processing idea is super easy. We have main `FormHandler` object, which will validate data and
8682
run some handler (right now we have only one Handler - email sender). And as the result we can get info
@@ -101,15 +97,15 @@ $result = $form->response();
10197
// TODO: do somethign with the results. For example write to a session and redirect back.
10298
```
10399

104-
### 5. Set Configurations
100+
## 4. Set Configurations
105101

106102
As you can see above we need to set 3 configuration arrays:
107103

108104
* `$validationRules` - defines validation rules and messages
109105
* `$mailerConfig` - defines mailer component (PHPMailer or Mailchimp) and it's params
110106
* `$messageConfig` - defines From/To/Body fields
111107

112-
#### 5.1. Validation Rules
108+
### 4.1. Validation Rules
113109

114110
For validation we use popular [Valitron](https://github.com/vlucas/valitron) PHP library. We use
115111
`mapFieldsRules()` method to set fields rules and `labels()` method to set field labels to show
@@ -135,7 +131,7 @@ $validationRules = [
135131
];
136132
```
137133

138-
#### 5.2. Mailer Config
134+
### 4.2. Mailer Config
139135

140136
There are two options for Mailer: [PHPMailer](https://github.com/PHPMailer/PHPMailer) and implementation
141137
of [Mandrill API](https://mandrillapp.com/api/docs/).
@@ -163,9 +159,9 @@ $mailerConfig = [
163159
];
164160
```
165161

166-
#### 5.3. Message configuration
162+
### 4.3. Message configuration
167163

168-
Final configuration you have to set is options for your email: From, To addresses; Subject and Body.
164+
The latest configuration you have to set is options for your email: From, To addresses; Subject and Body.
169165
Optional you can set CC and BCC headers as well.
170166

171167
Example:
@@ -182,16 +178,16 @@ $messageConfig = [
182178
];
183179
```
184180

185-
For each address you can set numerous emails in such format:
181+
For each address field you can set numerous emails in such format:
186182

187183
[ email1 => name1, email2 => name2, ... ]
188184
OR
189185
[email1, email2, email3 ...]
190186

191-
`bodyTemplate` and `altBodyTemplate` are path to usual PHP template files, which will be used to generate
192-
message body part.
187+
`bodyTemplate` and `altBodyTemplate` are paths to usual PHP template files, which will be used to generate
188+
email message (HTML and plain versions accordingly).
193189

194-
### 6. All together
190+
## 5. All together
195191

196192
If we combine all parts we can get file similar to this one:
197193

@@ -256,7 +252,7 @@ exit;
256252

257253
In this example we write errors to cookies to be able to get them on the HTML page via JavaScript or PHP code.
258254

259-
### 7. Body templates
255+
## 6. Body templates
260256

261257
Templates are usual PHP files, which can print any PHP code you leave inside. However to make editing
262258
easier we added tokens support. So any keys, which are passed as data to FormHandler can be used as a
@@ -453,17 +449,17 @@ Some forms may have multiple fields, like checkboxes, multiple selects or dynami
453449
Example:
454450

455451
```html
456-
<!-- multiple select -->
457-
<select name="choice" multiple>
458-
<option value="1">1</option>
459-
<option value="2">2</option>
460-
<option value="3">3</option>
461-
</select>
462-
463-
<!-- text inputs -->
464-
<input type="text" name="links[]">
465-
<input type="text" name="links[]">
466-
<input type="text" name="links[]">
452+
<!-- multiple select -->
453+
<select name="choice" multiple>
454+
<option value="1">1</option>
455+
<option value="2">2</option>
456+
<option value="3">3</option>
457+
</select>
458+
459+
<!-- text inputs -->
460+
<input type="text" name="links[]">
461+
<input type="text" name="links[]">
462+
<input type="text" name="links[]">
467463
```
468464

469465
You can validate each input using wildcard field name inside validation rules:
@@ -511,3 +507,8 @@ field in curly braces. For example, in the file 'template-html.php':
511507
</body>
512508
</html>
513509
```
510+
511+
# Examples
512+
513+
You can check working examples inside `examples` folder of the package, start your investigate from `index.php` file (contains forms HTML).
514+
There you can find which files loaded next, when you submit forms.

0 commit comments

Comments
 (0)