From our monthly sponsor: Ship customer-facing metrics fast with Keen.io. Collect, store, query, & display stunning analytics.
Here’s an implementation of a contact form with PHP and jQuery. Usually, one would only want the message posted by the user to be sent to an email address. This script does that and also saves all these messages in the database for the admin to keep track of them.
The table to store this data is the following:
CREATE TABLE CONTACT( pk_contact INT UNSIGNED NOT NULL auto_increment, name VARCHAR(120) NOT NULL, email VARCHAR(120) NOT NULL, website VARCHAR(120) NOT NULL DEFAULT "", message VARCHAR(300) NOT NULL, added_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, primary key(pk_contact) )type=innodb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
We will keep track of the name, email, website (optional), the message, and the date when the message was sent. Feel free to change the limits of the fields, but don’t forget to change them as well in the PHP files.
We use jQuery for the front end, and AJAX for submitting the form. All the validations are done on the server side, and the user will be notified when one of the fields is not correct. If the message is inserted into the database, an email is sent to the admin, notifying him about a new message.
We provide two CSS templates for the contact form.
Concerning the installation, all you need to do is run the database script (db_main_script.sql) and configure your database connection in config.php:
define('DB_SERVER', 'localhost'); define('DB_USER', 'root'); define('DB_PASS', ''); define('DB_NAME', 'ContactForm');
You can also configure the email parameters in this file, like the Email Message Subject.