The HTML code of the form is given below:
In the contact form download, the form code is in the email-contact-form.html page. To embed the form in a web page, just copy and paste the HTML form code to the web page.
Validating the form submission
Form validations are essential for any web form. For this simple contact form, we will make all the fields mandatory and will make sure that the email field is in the format: email@example.com.
Here is the client side form validation code:
Once the contact form is submitted, the form submission data is sent to the script mentioned in the action attribute of the form (contact-form-handler.php in our form). The script then will collect the form submission data, validate it and send the email.
The first step is to validate the data. Ensure that the mandatory fields are filled-in, and that the email is in proper format.
The server-side code is given below:
$myemail = ‘firstname.lastname@example.org’;//if(empty($_POST[‘name’]) ||
$errors .= “n Error: all fields are required”;
$name = $_POST[‘name’];
$email_address = $_POST[’email’];
$message = $_POST[‘message’];
$errors .= “n Error: Invalid email address”;
Emailing the form data using PHP
We will now compose and send the email.
$to = $myemail;
$email_subject = “Contact form submission: $name”;
$email_body = “You have received a new message. “.
” Here are the details:n Name: $name n “.
“Email: $email_addressn Message n $message”;
$headers = “From: $myemailn”;
$headers .= “Reply-To: $email_address”;
//redirect to the ‘thank you’ page
We first check whether the validations succeeded. If there were errors, the email is not sent. The PHP mail function is used to send the email. After sending the email, the visitor is redirected to the ‘thank you’ page.