|
|||
|
|
|||
|
|
Free, simple, PHP based email contact formThis page presents the sample code for a simple 'contact us' form. Such a form is usually present on the "contact us" page of websites. This form collects some information from the visitor and emails to the site admin, after validating the data. In this section we shall discuss, how to implement it.The HTML contact form codeThe very first step is the creation of HTML form that will collect the user information. For the sake of simplicity we are considering a simple contact form with only 4 fields. Let us consider that we want to collect the user information like name, email address, postal address and message. In this case the HTML required to create this form will be as follows:
Validating the form submissionAfter entering the required information in the fields of HTML form on "email-contact-form.html" and hitting the "Submit" button, user will be taken on "contact-form-handler.php". This is the PHP page that will contain the code required to do the rest of the operation.Before sending the form data to the admin of site, it is necessary to validate the data. For example, if a user simply hits the "Submit" button, without entering any information, empty email will be sent to the admin. Another example is the use of fake email address in the "email address" field. We should place some PHP code in contact-form-handler.php that should validate the data. If the data is not valid, user should be redirected to "email-contact-form.html" again, so that he must enter the correct information. If the data is valid, then email should be sent to admin. Below is the PHP code that will check the data of all fields of contact form. The email address field will be checked for the proper syntax. This code will also check if "name", "postal address" and "message" fields contain some data or not. In case of empty data or wrong email syntax, an error message will be displayed on the screen with a link back to "email-contact-form.html".
This code is getting the form values in PHP variables and validating them. The form values are extracted from the POST array, because we had set the "method=POST" while defining the form. After getting the form values, we have defined a variable "$error_occured". The value of this variable is "no" in start. In case any error occurs, its value is changed to "yes". We shall check the value of this variable before sending the email. If the value is "yes", then email will not be sent and the user will be returned to previous page. We have used the PHP's empty() function to check if the variable is empty. For validating the email address, regular expression(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$) is used. This expression is checking the syntax of the email address entered. Emailing the form data using PHPNow we shall use the above information to compose the email and send to the admin of the site.
In the above code we have defined the variables that are required to send the email. This piece of
code is starting with an "if" condition. Here we are checking the values of the variable, "$error_occured". If value is "no", then
it means that there were no errors and the email will be sent. If the value is "yes", then it means that there were some errors.
In this case, the email will not be sent and user will be simply asked to correct the errors. Download the code for the contact formClick here to download php-email-conact-form.zipThere are two files in the zip file. One is "email-contact-form.html". It is holding the simple HTML contact form with a submit button. The other page is "contact-form-handler.php". This is the PHP page that is getting the data, validating the data, reporting the errors (if any) and sending the email. Why to have a contact form?The contact form allows your web site visitor to send the email messages through the web interface. Your web site visitors could be asked for the views about the services or products offered. The visitors can also ask questions about the product or services. They can submit their suggestions too.Contact forms are the easiest way of communication between the visitors and the webmaster. See also: Secure your HTML contact form using captcha
Related pages |
| Copyright © 2008 html-form-guide.com . All rights reserved. | ||||