This is my first real tutorial (more commented code than a real tutorial), and is as much for myself as everyone else (I’m using the blog as a sort of resource store for myself, with the added bonus that everyone else can see it too!).

Anyway, the idea here is some explained code to produce a simple online form which will email a person/s the data from the form. It also includes validation to ensure that the form can not be sent unless the data is precise and relevant.

On to part one – The Form and the mail functionality.

The first thing we need to do is create the form itself for the user to put there data in.

<form method="post" onsubmit="return validate(this);" action="formPage.php"> <input id="name" size="50"/> <input id="company" size="50" /> <input id="contact" size="50" /> <input id="telephone" size="50" /> <textarea cols="38" rows="5" id="description"></textarea> <input id="quantity" size="4" /> <input value="Submit"/> <input value="Reset" /> </form>

The opening form tag contains method=”post” used to actually post the data, onsubmit which is used to call the validation function and action which links back to itself. The .php file in the action will have to be renamed the same as the page the form is being displayed on. There are two buttons at the bottom (‘input values’) one of which will submit the form (method, onsubmit and action) and the other will clear all data from the form. Simple!

Next, the code to email the data to the admin:

<?php

if(isset($_POST['contact'])){ $admin = "recipient@sendToEmailAddress.com"; $name = $_POST['name']; $company = $_POST['company']; $contact = $_POST['contact']; $telephone = $_POST['telephone']; $description = $_POST['description']; $quantity = $_POST['quantity']; mail($admin, 'Subject Title', "This email was generated using the Product Resource Form at www.johnalexanderrowley.com.\n Name: $name Company: $company Email: $contact Telephone: $telephone Description: $description Quantity: $quantity", 'from: fromAddress@fromaddressEmail.com'); ?> <script> alert("Thank you for using the formPage.\n Your request has been sent to the Relevant person."); </script> <?php } ?> The IF statement checks if the input field (in this case 'contact') has been filled in. If it has then the following mail code is ran. This stops the form submitting when the page is loaded.

The various variables are set, including the ‘$admin’ variable which contains the email address that the data is to be sent to. The other variables pull in the previously posted form values. The form ‘input id’ is the text contained within the square brackets in each case.

Now the variables have been set the mail can actually take place; The ‘mail’ php function is called which requires a recipient, subject title, content and header information separated by commas. These are defined in the normal brackets using the various variables. Note that the content is comprised of various variables and no commas are used.

Finally, a javascript script is used to display a thank you message, and the IF statement is closed!

Hay presto, the form is created and will email the admin.

To add validation to the form, please view Part 2!