PHP/MySQL Contact Form with jQuery
Development March 12, 2010 by chadking 10 Comments
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.












This isn’t a bad tutorial, but it could have been better. The main thing I would suggest is to use PHPMailer with support for SMTP. Since most shared servers will have mail() either shut down entirely or require it to be sent via SMTP.
Also, using a different MySQL class something like ezsql. Or even use MDB2 from PEAR (but though some shared hosts don’t have PEAR installed).
It’s a good article, and for the ones looking for a intro into forms and such then it’s a good move. We use CakePHP at http://theeasyapi.com which includes exceptional lightweight API access.
Nice work though
Useful and simple. Thanks for sharing
Nice tutorial, easy to follow, thanks.
Thanks for this tutorial, very nice
i don’t see the tutorial but any the demo is beautiful will study the download source thanks
Thanks it’s looks very nice
esta muy bueno.. graciass
thanks nice Contact Form
I got an issue with the form.
I tried to embed it into my MVC-Framework and it doesn’t work anymore.
I click on the submit button and get the loader image, but nothing happens next.
i read all the files and nothing seems to have a wrong link to a resource file.
ThankS Nice Contact Form
DankeSchon : )