Providing a simple contact form could be a big security issue if the form script doesn't have any validation or security features. In the past it was very common to use simple mail scripts to post a web form to some e-mail address. A lot of the spam getting people in their mail box is send with vulnerable form mail scripts.
The tutorial below is based on the older classes of PHPmailer and Xajax. Visit our forum for the updated (full) Xajax tutorial example code. If you don't like to use the Xajax script, there is a jQuery based Ajax contact form tutorial on the Web Development Blog
Using Xajax
For those people thinking Ajax is a Dutch Soccer club, I must say they are right! But Ajax is more, we are using in this tutorial Xajax a very powefull and easy to use PHP Class library which makes it possible to execute a PHP script using XML and some JavaScript code. Get the latest Xajax library here.
Sending mails with PHPMailer
PHPMailer is very popular PHP mail script with a lot of possebilities: Send mails via SMTP, sendmail, Qmail or the with PHP mail function. The class is able to handle multiple attachments and can be used for full featured e-mail clients, download the class here.
We have created a demo to show how the form does looks like.
OK, lets start... first we need to include the class files for phpmailer and Xajax, note in this tutorial are all files located in one folder. The class file folders are one level above:
require_once('xajax/xajax.inc.php');
Next we have to build the function which has to be executed if the user hits the submit button. The function has the following parts:
- Create a new Xajax response object
- Form field validation (e-mail address validation), if not valid the user gets a message
- Creating the PHPMailer object and define the required vars for the e-mail task
- Sending the mail message...
- the response (replace the form with some message) is send to the browser