A simple AJAX Contact Form with jQuery and PHP Validation
Posted on September 2, 2008, Filled under AJAX, JQuery, PHP,
Bookmark it
Thanks for visiting our website! We regularly publish posts like this one. If you are interested in receiving the latest updates as soon as they are posted, please consider subscribing to the RSS feed or to our e-mail newsletter.

The aim of this tutorial is to help you to create a simple (tableless) contact form using AJAX, JQuery & PHP. We will have a HTML page which will contain the form, a CSS file, a php page where the data will be sent and another file where the validation function(s) will be located. JQuery is a new and powerful library which simplifies the way that you write JavaScript.
Step 1 – Creating the configuration file
config.php
<?php
// To
define("WEBMASTER_EMAIL", 'your_name@domain.com');
?>
Here you should fill the e-mail address where you wish to receive the mail as well as the address where you wish to receive the replies (usually the same).
Step 2 – Creating the css file
style.css
/*
Credits: Bit Repository
CSS Library: http://www.bitrepository.com/
*/
html, body { padding: 0; border: 0px none; }
.notification_error
{
border: 1px solid #A25965;
height: auto;
width: 90%;
padding: 4px;
background: #F8F0F1;
text-align: left;
-moz-border-radius: 5px;
}
.notification_ok
{
border: 1px #567397 solid;
height: auto;
width: 90%
padding: 8px;
background: #f5f9fd;
text-align: center;
-moz-border-radius: 5px;
}
.info_fieldset { -moz-border-radius: 7px; border: 1px #dddddd solid; }
.info_fieldset legend
{
border: 1px #dddddd solid;
color: black;
font: 13px Verdana;
padding: 2px 5px 2px 5px;
-moz-border-radius: 3px;
}
.button
{
border: 1px solid #999999;
border-top-color: #CCCCCC;
border-left-color: #CCCCCC;
background: white;
color: #333333;
font: 11px Verdana, Helvetica, Arial, sans-serif;
-moz-border-radius: 3px;
}
/* Label */
label {width: 140px; padding-left: 20px; margin: 5px; float: left; text-align: left;}
/* Input, Textarea */
input, textarea
{
margin: 5px;
padding: 0px;
float: left;
border: 1px solid #999999;
border-top-color: #CCCCCC;
border-left-color: #CCCCCC;
color: #333333;
font: 11px Verdana, Helvetica, Arial, sans-serif;
-moz-border-radius: 3px;
}
/* BR */
br { clear: left; }
Step 3 – Creating the html contact page
This is the page where the AJAX call to contact.php is made. I will explain you the JavaScript Code in detail below:
contact.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>TableLess Ajax Contact Form (Demo)</TITLE>
<meta name="Keywords" content="form, divs">
<meta name="Description" content="A CSS Tableless Ajax Contact Form">
<!-- JQuery -->
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
// we will add our javascript code here
$(document).ready(function(){
$("#ajax-contact-form").submit(function(){
// 'this' refers to the current submitted form
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "contact.php",
data: str,
success: function(msg){
$("#note").ajaxComplete(function(event, request, settings){
if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
{
result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
$("#fields").hide();
}
else
{
result = msg;
}
$(this).html(result);
});
}
});
return false;
});
});
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<center>
<center><h3>A Tableless Contact Form (JQuery & AJAX)</h3></center>
<div align="left" style="width: 500px;">
<fieldset class="info_fieldset"><legend>Contact</legend>
<div id="note"></div>
<div id="fields">
<form id="ajax-contact-form" action="javascript:alert('success!');">
<label>Name</label><INPUT class="textbox" type="text" name="name" value=""><br />
<label>E-Mail</label><INPUT class="textbox" type="text" name="email" value=""><br />
<label>Subject</label><INPUT class="textbox" type="text" name="subject" value=""><br />
<label>Comments</label><TEXTAREA class="textbox" NAME="message" ROWS="5" COLS="25"></TEXTAREA><br />
<label> </label><INPUT class="button" type="submit" name="submit" value="Send Message">
</form>
</div>
</fieldset>
</div>
</center>
</body>
</html>

Once the DOM is loaded (we determine this by using $(document).ready()), the script checks if the user submitted the form by using $("form").submit(). If it is, the form elements (names & values) are serialized in order to pass them to the AJAX Call:
var str = $("form").serialize();
$.ajax() takes one argument, an object of key/value pairs, that are used to initalize and handle the request, and returns the XMLHttpRequest object for post-processing (like manual aborting) if needed.
Some options are used to control the behaviour of $.ajax. The information is from JQuery Documentation:
(String) type – This is to determine the type of request that is made: ‘POST’ or ‘GET’. (default is ‘GET’)
(String) url – The URL to request
(Object|String) data – Data to be sent to the server. It is converted to a query string, if not already a string. Is appended to the url for GET-requests. See processData option to prevent this automatic processing.
(Function) success – A function to be called if the request succeeds. The function gets passed one argument: The data returned from the server, formatted according to the ‘dataType’ parameter.
To find out more about $.ajax consider visiting http://docs.jquery.com/API/1.1/AJAX.
The request succeded? In this case we will call the ajaxComplete(callback) function, which executes whenever an AJAX request is completed. The XMLHttpRequest and settings used for that request are passed as arguments to the callback.
If ‘OK’ is returned the form hides and a thank you message is shown. Otherwise, the script will show the errors.
$.ajax({
type: "POST",
url: "contact.php",
data: str,
success: function(msg){
$("#note").ajaxComplete(function(event, request, settings){
if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
{
result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
$("#fields").hide();
}
else
{
result = msg;
}
$(this).html(result);
});
}
});

In the next page of our tutorial we will create: functions.php (which has the e-mail address validator) and contact.php (which is the script that parses the submitted data).
Step 4 – Creating the functions’ script.
functions.php
<?php
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/
$regex = '/([a-z0-9_.-]+)'. # name
'@'. # at
'([a-z0-9.-]+){1,255}'. # domain & possibly subdomains
'.'. # period
"([a-z]+){2,10}/i"; # domain extension
if($email == '') {
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}
return empty($eregi) ? true : false;
}
?>
This file contains only the e-mail address validator, in our sample, but you can use it to add more (validation) functions which can be used in the contact.php page.
Step 5 – Creating the page which parses the data
contact.php
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/a-simple-ajax-contact-form-with-php-validation.html
*/
include 'config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'functions.php';
$name = stripslashes($_POST['name']);
$email = $_POST['email'];
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.';
}
// Check message (length)
if(!$message || strlen($message) < 15)
{
$error .= "Please enter your message. It should have at least 15 characters.";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">rn"
."Reply-To: ".$email."rn"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
In contact.php, we have to determine first if a POST request was sent to the file. If $post is not null, the functions.php file is included (containing the e-mail validator) and the POST fields are declared. Each required field is checked. If any error is found, the $error variable will be filled with that error message. At the end, if $error is null (as it was initially) & the mail is sent, contact.php will output ‘OK’ (which is checked in contact.html). It is is not null, contact.php will output the error(s), which will be shown, of course, in contact.html.
Select the type of item you would like to download:
- September 2, 2008
- article by Gabriel C.
- 208 comments
Sponsors
Related Posts
-
A PHP Contact Form with JavaScript Real Time Validationat April 30, 2009 with 18 comments
-
AJAX Contact Form with CAPTCHA, Realtime Validator and PHP Backendat February 8, 2010 with 69 comments
-
How to Implement a jQuery AJAX Login Form into a Modal Boxat April 24, 2009 with 37 comments
-
An AJAX (jQuery) Username Availability Checker with PHP Back-endat December 5, 2008 with 32 comments
-
Creating an AJAX Login Form using MooToolsat December 21, 2008 with 26 comments



208 Replies to "A simple AJAX Contact Form with jQuery and PHP Validation"
September 16, 2008 at 6:15 AM
a working demo would be nice
September 22, 2008 at 4:10 AM
Yes, it would be, but the script has the mail() function which can be used abusively (i.e. SPAM). If a demo will be made, it must have some protection.
September 29, 2008 at 1:24 PM
A demo is now available.
October 9, 2008 at 9:47 PM
Thank you very much for your Tutorial. But it has a small Validation error in e-mail field.
info@yahoo,com
i mention comma in the above Email address.. But it doesent validate your code..
plz check and correct it..
Thank you
October 10, 2008 at 4:02 AM
Thanks for noticing. I’ve fixed the error by adding a backslash before the period sign.
October 23, 2008 at 8:18 AM
hi, I tried it but it doesn’t work with ie6
October 23, 2008 at 8:26 AM
sorry, I forgot to put my phpserver on,
it’s working well with ie6
October 23, 2008 at 8:31 AM
Good, I was quite surprised when I read your first comment, because I know that JQuery is compatible with IE6.
October 25, 2008 at 8:28 AM
[...] Tableless Ajax Contact Form With jQuery (Tutorial & Files) – Demo [...]
October 26, 2008 at 9:04 AM
[...] Tableless Ajax Contact Form With jQuery | Demo [...]
October 28, 2008 at 7:48 PM
How might I get this to put the user’s email into the “from” field properly so that a REPLY goes back to them instead of to me? In my use, this would be considerably more useful. I’ve tried putting $from and $email into the config file, but that doesn’t do it. I don’t know enough about PHP yet.
October 29, 2008 at 4:06 AM
Replace
from line 54
with
I’ve also updated the post.
November 3, 2008 at 9:53 PM
Hi,
Awesome site, thanks so much!
I’m using your script for an email sign up form. How would I go about forcing an email subject when the user doesn’t have the option of entering a subject.
Thank you so much
November 4, 2008 at 3:27 AM
If you don’t need a subject from the person filling the form & want to set a specific subject you can do the following:
1. Remove line 76 from the form (the one with the subject)
2. Remove line 19 from contact.php
3. Change $subject from line 58 (where you have the mail function) with ‘your subject here’.
November 6, 2008 at 12:30 AM
thanks
November 6, 2008 at 7:09 AM
hi webmaster, i have downloaded your script but its not working
nothing is happening when i click on submit button
iam using php4 and testing under localhost do i need to change any code if any please post me
thank u
November 6, 2008 at 7:22 AM
What browser are you using (type & version)? Please take a look at this page: http://docs.jquery.com/Browser_Compatibility. Perhaps there’s a compatibility problem between JQuery & your browser. Did you see the demo in action before downloading the files?
November 8, 2008 at 5:59 PM
Great! That modification worked without a problem.
Now, I’d like for the person’s name to be put in the From: field properly. They’re typing their name into the form, but I don’t receive it. I did replace $from with $name (I forget where) and it puts it into the proper field… but, it appends @mydomain.com after it. Other than that, this script does exactly what I needed with a minimum of hassle. Thanks a lot!
November 8, 2008 at 6:18 PM
I’ve update the tutorial. Check line 53 from the contact.php page
November 11, 2008 at 9:27 PM
Hi,
This is great website, keep it up
I’m just wondering how I would go ahead and force a sentence such as “The following email would like to sign up to the Newsletter” in the body of an email?
Also is it possible to have a checkbox submit a true or false?
Starting to get more in PHP coding… sites like theses are a great help. If there’s anyway I can donate some money for your help, please let me know
Thanks.
November 12, 2008 at 5:21 AM
Hi,
Thank you for your comments regarding this website. Let’s add the checkbox first.
1. Here’s the code that should be inserted in contact.html:
I recommend you to add it just AFTER:
2. Now, we have to edit contact.php. AFTER:
add the following line:
3. We will make the final modifications.
Look for:
$mail = mail(WEBMASTER_EMAIL, $subject, $message, "From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion());Just BEFORE it add the following code:
if($newsletter_signup) { $message .= "\n\n"."[The following email would like to sign up to the Newsletter.]"; }Notice how I used the string concatenation to add additional text to the message.
I hope you will manage to successfully implement the code I gave you. If you have any questions, let me know.
If you’d like to make a donation, you can look for a PayPal button located just above the pagination links for this tutorial.
November 15, 2008 at 11:14 AM
Thank you for this great form.
How would you change the submit button so when a user clicks on it, it changes to something like ‘Submitting Form, please wait…” With the dots animated.
November 15, 2008 at 11:55 AM
This form submits fast enough and a loader (until you the script gets a response from ‘contact.php’) is not really needed. However, it can be useful if the user has a slow bandwidth connection. Here’s how you can add one:
1. AFTER:
ADD the following div:
2. AFTER:
$("form").submit(function(){ADD the following code:
$("#submit").hide(); $("#loading").html('Loading...');This way the submit button is hided and the loader is shown.
3. BEFORE:
ADD:
$("#loading").hide(); $("#submit").show();The loader is hided and the submit button is restored.
As for the dots you can use an animated GIF:
$("#loading").html('Submitting Form, please wait <img src="path/to/dots.gif" align="absmiddle">');November 18, 2008 at 3:52 PM
Thank you, works great!
November 28, 2008 at 9:54 AM
Sure, here’s what you have to do:
1. Replace the following line from the contact.html page:
WITH this code::
2. Now, you have to edit contact.php. Find this line:
Replace it with:
Find:
if(!$error) {AFTER it, ADD:
3. Let’s add some validation to the new fields! Find:
// Check name if(!$name) { $error .= 'Please enter your name.'; }Replace it with:
// Check first name if(!$first_name) { $error .= 'Please enter your first name.'; } // Check last name if(!$last_name) { $error .= 'Please enter your last name.'; } // Check company if(!$company) { $error .= 'Please enter your company name.'; }To make a field optional, remove the conditional IF that verifies it.
November 28, 2008 at 4:32 AM
Great tutorial! I was wondering if i could send additional information to the email, for example i have a field called “Company” and would like it to be in front of the subject (along with the first- and last name).
November 28, 2008 at 4:44 AM
Do you want to replace the “name” field with 2 fields (first & last name) and to add an additional field called “company”? Would the subject be something like: “Company Name: First Name, Last Name”?
November 28, 2008 at 8:06 AM
Yes that’s exactly what i need. Do you know how to do this?
November 28, 2008 at 12:37 PM
Thanks so much! Once again, great tutorial. And the support is awesome
December 17, 2008 at 10:38 AM
It doesn’t validate for sub-domain email addresses.
Like my email address:
beyblade[- at -]quickcms [- dot -]exofire[- dot -]net
December 17, 2008 at 11:41 AM
It does now
Thanks for the tip!
December 17, 2008 at 9:17 PM
Hi,
I just tried it on my website and it seems to be working fine but I am not getting any email
I have been waiting for the last 30 minutes now. I added my email twice in the config file, is there anyplace else where I need to add it? I had to copy paste the codes into files because my zip extractor wont open the zipped file downloaded from here either
December 17, 2008 at 9:43 PM
For some reason when I am using this on my site, when I press ‘Submit’, js alert window comes up saying ’success!’. But doesn’t do anything after. Also if I remove the [HTML STRIPPED] line it still does nothing.
December 17, 2008 at 9:57 PM
Oh please disregard the earlier message. I got the email after an hour! Thank you so much for this awesome script!!
December 18, 2008 at 2:22 AM
@Travis O'Donnell: After checking the contact form that you installed in your site, I've noticed that jquery-1.2.6.js is not loaded. Are you sure the file is in the main root of your site? I've tried to open it but I got an error.
August 15, 2009 at 4:59 PM
@Gabriel,
Im having the same problem. even without anything type in the form, when I click submit a window pops up with “success” nothing is validated or nothing, no error messages…
Plz help
August 15, 2009 at 10:49 PM
@anthony, this issue was already discussed. As I mentioned before, you just did not do something right (the JQuery library is not included, the ID of the form is not the right one etc.). Just check the older comments and hopefully you will find the answer. If you want to me setup the form on your website I would do it if you are willing to pay a fee.
December 23, 2008 at 7:30 AM
Thanks for helping me earlier on with the subject field. I was wondering how i could send a overview of all the fields in the email, like:
- Company:
- First name:
- Last name:
Hope you can help me out with this one!
Greetz,
Jasper
December 26, 2008 at 3:29 PM
You need to modify $message. Example:
$message = "- Company". $company."\n- First name: ".$first_name."\n- Last name: ".$last_name."\n\n".$message;
August 23, 2009 at 7:07 AM
Which file do we edit this on? contact.php or feedback.php?
That string ” $message ” exists in many spots in different files.
Thanks much for your help!
August 24, 2009 at 1:30 AM
In contact.php: add the code after
if(!$error) {(just before the mail() function is called). The file feedback.php was just used for demo purposes to demonstrate how you can add more AJAX Forms in the same page.August 24, 2009 at 4:16 AM
i eventually figured it out.
December 26, 2008 at 11:16 PM
Nifty! Thanks for the great example!
December 27, 2008 at 7:18 PM
Hi,
I've been trying to make this work for the last two days now and it shows the thank you message but I dont get anything in my email. I tried different email accounts, both yahoo and hotmail. I didnt change any code in either of the pages. Can you look at it and tell me whats wrong if you have time? The link is http://www.ranbirkapoor.net/catb/contact.html
Thanks!
December 28, 2008 at 3:02 AM
This has nothing to do with the script, which is working fine. It's probably a problem with the software that is installed on your server and sends the mails. Did you use mail() before in other scripts? Did it work? Also, make sure that you didn't change the original script and you typed the correct e-mail address in the config.php file.
December 29, 2008 at 10:24 PM
This is amazing, just what I needed!! Great job! I had a question… What if I have two forms on my site? One for comments and one for ordering. How can I modify the script so it will work with two different forms with different fields?
Thanks so much!
December 30, 2008 at 4:41 AM
Madiha, good question! I have updated the ZIP file. If you download it, you will find a page sample with 2 forms in the same html page (multiple_forms.html). I will also update this tutorial with an explanation of how you can use more then 1 form in a page.
December 30, 2008 at 9:28 AM
Hi sorry but the zip file does not work, it says there are 0 files in the folder…can you please check for me, I am pretty sure its empty…
Thank you so much for ur help!
December 30, 2008 at 9:39 AM
I've just downloaded the ZIP file (21,1 KB) and extracted it successfully. What software are you using to extract the ZIP archive?
December 30, 2008 at 9:40 AM
Hi, I am using Z-Zip and I also installed Winzip and winzip error is saying "File corrupt, possible transfer error…"
hmmmm…
December 30, 2008 at 9:52 AM
I will send you the ZIP via e-mail and find a solution for this transfer error problem.
December 30, 2008 at 11:37 AM
Thanks alot! It worked great!! This is the best form out there!
I had a quick question about an answer that you posted for somebody here…
The Question was, How do we add Company name, First name, Last name to the email we recieve inside the body of the email?
And you said:
You need to modify $message. Example:
$message = "- Company". $company."\n- First name: ".$first_name."\n- Last name: ".$last_name."\n\n".$message;
But in my contact.php file, I have this info already placed there:
"$message = htmlspecialchars($_POST['message']);"
How do I add that new line in?
Thank you for all your help!
December 30, 2008 at 11:48 AM
Yes, before the line with the mail function add:
$message = "- Company". $company."\n- First name: ".$first_name."\n- Last name: ".$last_name."\n\n".$message;
This way, you will have company, first & last name before the actual message. Notice how I used string concatenation in this situation.
December 30, 2008 at 2:47 PM
Hi, thank you that worked!
But now, it wont validate if a message was input and if it was less than 15 char..
It wont give me an error if i enter NO message…
=/
December 30, 2008 at 2:51 PM
Did you remove $message = htmlspecialchars($_POST['message']); ? I said that you should add that new line before the call to mail() function. I don't know how you modified contact.php. The validation should work 100%!
December 30, 2008 at 2:59 PM
This is what I have:
$message = "- Name: ".$name."\n- Phone: ".$phone."\n\n".$message; htmlspecialchars($_POST['message']);
December 30, 2008 at 3:04 PM
No, that is wrong! I told you to add the new line before the call to mail() (just after if(!$error) ) without any other changes.
December 30, 2008 at 3:21 PM
Ohhh. Im sorry lol. I misunderstood. Okay I tried that now it works!!
=) Thanks alot for all your help!
January 7, 2009 at 3:19 AM
That's great! The strange is though i don't see the actual "message".
I do see company, firstname, lastname. What am i doing wrong?
January 7, 2009 at 5:41 AM
I don't know how you edited that file. But if you followed my instructions it should have worked.
January 8, 2009 at 10:08 AM
You right, my bad!!
January 17, 2009 at 10:41 PM
Thank you very much for a clean and thorough presentation!
Just wanted to note I had to change (in contact.php – Line 20):
$message = htmlspecialchars($_POST['message']);
TO:
$message = stripslashes(htmlspecialchars($_POST['message']));
because, for example: "user's typed message" was sending me: "user\'s typed message". I don't know if it is my server's PHP settings (which are default from my provider) or what, but I had to add stripslashes to the $message line, now I am good.
January 18, 2009 at 8:01 AM
Yes, you did good. I just omitted to add it for "message", as I did for "name" and "subject". Will update the post!
January 19, 2009 at 9:52 AM
How can i validate a select option?
January 19, 2009 at 10:18 AM
This is what i have:
<select name="aanhef" id="aanhef">
<option value="De heer">Man</option>
<option value="Mevrouw">Vrouw</option>
</select>
And i can't send the form because the select field is empty.
Can you please help me out?
January 20, 2009 at 2:32 AM
Fixed the problem i guess , sorry!
January 20, 2009 at 3:48 AM
@Jasper: That dropdown ("aanher") doesn't really need a validator because there are only 2 options in it and none of it is NULL.
January 20, 2009 at 5:25 AM
Could i add a CC adres so that it mails to 2 adresses?
January 20, 2009 at 7:06 AM
Or can i just ad an (comma seperated) address in the config.php
January 22, 2009 at 6:02 AM
Yes, you can either use CC or call again mail():
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
$mail_two = mail('your_second_mail@domain.com', $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
I do not recommend adding new email address separated by commas in the config.php. Everyone who receives the mail will see all the addresses where the mail was sent and in many cases this is not recommended at all.
January 24, 2009 at 10:22 PM
Cool.
I've been running this past a page with other Ajax calls, and once msg=='OK', the .ajaxComplete reacts to the successful completions of the other, unrelated Ajax events.
So, in contact.html:
27 $("#note").ajaxComplete(function(event, request, settings){
39 $(this).html(result);
…do you see any problem with eliminating line 27 and changing line 39 to this?:
39 $("#note").html(result);
Just curious, because I made that change and everything is still working in what I've checked: Safari 3.x, FF3, and IE7.
February 2, 2009 at 4:55 PM
@Ted: It shouldn't be a problem. It's good that you solved the issue by making this change.
January 27, 2009 at 4:42 AM
Oke that's cool. So can i send the second mail to the person who filled in the form? I'm not sure how to edit this:
$mail_two = mail('your_second_mail@domain.com', $subject, $message,
Is it something like this?
$mail_two = mail(". $email.", $subject, $message,
Hope you can help me!
February 2, 2009 at 4:50 PM
@Jasper: To send a mail to the person who filled the form consider using the following code:
February 1, 2009 at 9:30 AM
hi
i add succesfull your ajax form to my website but i have 2 issues
1 html specialchars are not suportet when i receive a mail it remove all special chars like àáâãäçèéêëìÃÂñòóôõö how i can solve this
2 the second is in the emails is nothing like name phone adress ect
this 2 issues also dont work in your downloadarchive i test it can you help me please thank you
February 1, 2009 at 8:35 PM
[...] Tableless Ajax Contact Form With jQuery (Tutorial & Files) – Demo [...]
February 3, 2009 at 9:59 AM
Thanks a lot! That works fine!
Just for the record, i donated a little bit
February 3, 2009 at 10:57 AM
i fixed by myself and add few functions
captcha enable disable in config.php
additional fields enable disable in config.php
autoresponder defne text in contact.php
add body text for received emails
fix for suport html special chars is working now
name
phone
adress ect.
ALL THIS CHANGES only for the contact formular not for the feedback
download here http://www.jobadoo.us/contact_form1.rar
February 4, 2009 at 4:44 AM
In the second email i don't want the same information as the first email.
The second email is sent to the person who fills in the form and should therefore contain a text message instead of all the filled in information.
But i'm not sure how to do this.
$mail_two = mail($email, $subject, $message,
so the field $message should be replaced with a text.
February 4, 2009 at 5:09 PM
i followed the steps correctly (or at least i think i did)
it looks good up till the end, when you click the submit button, this error appears above
\r\n" ."Reply-To: ".$email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { echo 'OK'; } } else { echo '
'.$error.'
February 6, 2009 at 7:45 AM
No worries! Allready fixed it!
I just added a second $message
February 12, 2009 at 1:53 PM
Excellent facility! I'm using AJAX contact forms for over 2 years, but I would like to either integrate JQuery within my current version or use other solutions like this one.
A bug I've discovered with your solution is that it reports some legitimate email addresses like invalid ones
For instance, my email address ends in [at] pro-it-service.com
The script seems to not recognize the dashes from the domain name…
Kind Regards,
Mihai Bocsaru
February 13, 2009 at 4:53 AM
Thanks for letting me know about this bug. I've removed it by updating the e-mail validator function.
February 13, 2009 at 4:58 AM
It's a pleasure and actually, thank you for such an excellent job!
February 13, 2009 at 10:31 AM
I guess you've updated it on your end, but didn't update it online as well or on the downloadable archive… I've just looked at the coding from this page and inside the archive and the email validating function is the same…
February 13, 2009 at 10:59 AM
I did update it online (tutorial & demo). I've also updated the ZIP archive. It should work now. I will re-upload the files again.
February 15, 2009 at 12:36 AM
Great, thank you! I've just discovered now that it is not compatible with Lightbox2 from http://www.lokeshdhakar.com/projects/lightbox2/
In case you place the lightbox 3 js calls before your ajax form js coding, then the ajax form will work, but the lightbox not and vice versa…
If you have the 3 lightbox js files referenced after the js coding for your ajax form, then the lightbox effect will work, but not your form AJAX effect…
Can you please try, see and fix that?
Cheers,
Mihai
February 15, 2009 at 12:42 AM
Lightbox2 is using Prototype Framework & Scriptaculous Effects Library. My form uses JQuery. You need to make this library compatible with other frameworks. Here's a URL that will help you: http://docs.jquery.com/Using_jQuery_with_Other_Libraries
February 15, 2009 at 12:50 AM
Excellent support! The process seems quite straight forward on http://docs.jquery.com/Using_jQuery_with_Other_Libraries
February 15, 2009 at 1:26 AM
Actually, I've thought about having both features on the same technology and so I've searched for Lightbox based on jQuery.
After some testing the best solution for me is http://plugins.jquery.com/project/jquerylightbox_bal because I could keep referencing images where I want the lightbox effect with rel="lightbox"
So folks, if you want to operate Lightbox and this awesome ajax form, consider http://plugins.jquery.com/project/jquerylightbox_bal instead of http://www.lokeshdhakar.com/projects/lightbox2/
I've used Lokesh Lightbox2 solution for 2 years, but it's now time to switch over to jQuery
February 15, 2009 at 4:10 PM
Thank's…..
February 25, 2009 at 4:27 PM
This is a nice article!
Thanks for sharing this.. keep it up!
March 3, 2009 at 11:56 PM
Great form with validation!
One question, if I wanted to validate a field for phone, where would i add this?
thanks!
March 4, 2009 at 9:16 AM
Great job!
March 4, 2009 at 8:30 PM
Hello. Amazing tutorial and amazing webmaster response here!
I’m a novice and have probably what is a basic question. Why this html code is included:
69. <div id="note"></div>
70. <div id="fields">
Because I do not see the ids #note and #fields in the CSS code.
Later
March 10, 2009 at 2:50 AM
The ID attribute is used to assign a unique identifier to an element. For instance “note” is assigned to a DIV where the submit status is shown (errors or message sent). If you study the JavaScript code you will see how ‘#note’ is used.
March 9, 2009 at 2:02 PM
Gabriel, awsome tutorial. I used your method on a site over the weekend and it is working well, so thank you. Just a thought/question…Would it be worth validating the form fields with jQuery as well as in the php file? That way the data will be validated even quicker. Let me know your thoughts.
Tom
March 10, 2009 at 7:03 AM
Ahh yes. With out that code it will not print out the Ok and Error messages. Thank you for your help Gabriel.
March 10, 2009 at 7:32 AM
There are 3 error messages that can be displayed. I want to have a different div for each of the messages instead of the one “.notification_error” div. I would need:
- .notification_name_error
- .notification_email_error
- .notification_message_error
how would I go about programming the java to allow this to occur?
Also have another question for you.
SCENARIO 1
also, lets say that someone screws up all 3 required fields and gets all 3 errors. I would like these errors to be displayed in their individual rectangular sausage shaped divs starting with
1) “.notification_name_error” at the top place
2) “.notification_email_error” in the middle
3) “.notification_message_error” on the bottom
SCENARIO 2
What if someone only screws up the email and the message fields. In that case I would want the “.notification_email_error” and “.notification_message_error” divs to appear but I would want the “.notification_email_error” div to appear in the top placement just like the “.notification_name_error” div in Scenario 1. The “.notification_message_error” div would appear in the 2nd, or middle, placement just like the “.notification_email_error” div in Scenario 1.
SCENARIO 3
If someone screws up just the message field then I would want the “.notification_message_error” to appear in the top placement just like the “.notification_name_error” div in Scenario 1.
How would one go about programming the java to attain this result?
Thanks again Gabriel,
IASIIS
March 17, 2009 at 4:17 PM
What you want is something quite different than what you currently have. First, you need to add the DIVs (with no text inside them) in the form (top, middle, bottom), each having a unique ID and the style set to ‘display: none’. You also need to change the contact.php file to send the response in JSON format. The JavaScript receives the data from contact.php and checks (for instance) if the email error notification exists. If the ‘email’ object exists, then set the notification_email_error to “visible” mode and fill the text error in it. I don’t know how familiar you are with PHP, JQuery and JSON but here are some URLs that can help you:
http://www.json.org/
http://www.php.net/json
http://docs.jquery.com/Events (JavaScript Events)
http://docs.jquery.com/Selectors (JQuery Selectors)
March 15, 2009 at 1:11 PM
Thanks for this great script.
I’ve implemented on a site that has more fields (12 in total), Name, Telephone and Email are required and the rest are not.
The problem appears when I complete only one field(and the error appears), then I complete the rest of the required fields but the submit button is disabled(doesn’t function anymore)
Also the email and telephone insert validation doesn’t seems to be working in my multiple field form(the same check functions gives me error as the field is not completed when in fact it is completed!
Any ideas? Thanks
March 16, 2009 at 4:31 PM
Hey, thanks a lot for this. I’ve been learning a lot about jQuery lately, and this was great.
I’m running into a problem with this however. Everything works fine except that when the message sends/returns successfully. The only thing that doesn’t is the success message.
The success message doesn’t appear and the fields div doesn’t get removed after a successful submission. It seems to just skip to the next code block in the javascript and returns/prints the ‘OK’ result from the php script instead of reading it correctly and changing content.
I was using jQuery 1.3.1, then switched to 1.2.6 just to see if that was the issue and I ran into the same problem.
Any help would be appreciated. Thanks!
March 17, 2009 at 4:19 PM
Did you change anything in the original code? Is the demo from Bit Repository working fine for you?
March 16, 2009 at 7:59 PM
Hi thanks for the form, this is the first time adding this to a site. I have an issue however, when i click submit all that happens is that instead of contact (at the top of the form) i get “OK” displayed and all the data in the form remains. Maybe i have done something wrong?
Thanks
Jamie
March 17, 2009 at 4:21 PM
I think you have modified contact.php. Did you add HTML code in it? The only thing that should be outputted from contact.php is “OK” (if successful).
March 17, 2009 at 7:01 PM
Gab, thanks for the response. Yes, I changed some code in the contact.php file because I needed to make some changes to integrate into my own setup. Nothing major. I’ve looked it over multiple times and didn’t see anything that could be causing an issue like this.
I did download the tutorial files and try them and they worked fine. Looking over those and this tutorial, there are a few differences in some of the coding. So, I’m going to use the downloaded files as a guide to redo some things and see if I can fix this.
March 17, 2009 at 8:26 PM
Ok, I’ve narrowed things down to the only thing that can be causing this issue.
Smarty!
There’s an issue with the Smarty templating system and your script that I need to figure out. If I take the success from the javascript and use that as the echo string instead of ‘OK’ in the contact php file, it will return that success div without any issues. That’s the quick fix, but the problem with that is the fields div doesn’t get removed. Not that big of a deal, but sort of unprofessional, especially since the data in the input fields are still there after a successful submission.
It seems as if the ‘OK’ echo string thats returned on success isn’t interpreted correctly by Smarty. Since it displays that echo string, I guess it takes it as a literal character. I tried taking the JS for this and putting it in an external JS file and linking to it in the header, but that didn’t change anything. Maybe I need to assign the return value as a smarty variable and then pass that to the JS somewhow. Bleh.
March 20, 2009 at 1:03 PM
Hey Gabriel,
Thanks for that info but I think it will take me some time to learn how to utilize JSON to reach the previously stated goal.
On another note, I have made some cosmetic changes to the contact form on my site. One of them was adding a 110 pixel wide background image (which contains a 55pix wide a:link state and a 55pix wide a:hover state) to the button div. I would like to create a rollover version so when someone is hovering over the button div the background image position will shift left by 55 pixels which will revealing the a:hover part of the image.
Some one from another forum suggested implementing JQuery event handlers that would be able to shift the background but, since I am a beginner I haven’t a clue how to work them in.
One other change I made was getting rid of the text which appeared next to each text fields. Instead I placed them inside each of the text fields so when the form loads you will see “name”,”email”, “subject” appear inside their related text fields. I can not get it so that the word “message” appears inside of the message div.
Any ideas on how to solve these two problems?
Here is the link to my contact form page:
http://caeious.com/stomatologdeblin/kontakt.htm
P.S. I have had to translate the OK and Error messages as well as “name”,”email”,”subject” into the Polish language so it may seem a bit strange but the form layout is structurally the same as you have in the tutorial. With Firebug you can see all the code. If you don’t have Firebug then I will post the code if you need it.
Thanks and take care,
IASIIS
April 15, 2009 at 2:01 PM
is it must to have a mail account on our own domain to use this script. can i get all mail through this form on my free gmail(not gmail apps) account
April 16, 2009 at 10:52 AM
Hi, great script got it working no problem. Just 1 quick question: I want to use the form as a kinda reservation form so I have added a drop down list of dates for the user to select. It doesn’t have to be validated, but I would like it to show up in the email. How do I get non-validated fields to be shown on the sent email?
Ta
April 21, 2009 at 1:02 PM
Hello,
Thanks a lot for this great tutorial, I was wondering, if I can use 2 forms on the same web page?
thanks in advance
April 24, 2009 at 3:06 PM
This topic was discussed before. In the zip archive containing the tutorial’s files you can find multiple_forms.html which has 2 forms in it. This way you will see how multiple forms can be added in the same page.
April 23, 2009 at 10:45 AM
Does anyone know how to attach and send a jpg file with the form?
April 24, 2009 at 2:39 PM
thanks a mil for this, works a charm!
April 24, 2009 at 4:01 PM
Thanks I didn’t watch it
April 25, 2009 at 6:11 AM
ADD:
$(“#loading”).show();
BEFORE:
$(“#submit”).hide();
So everytime you press Submit, the loading message will appear.
- In response to Gabriel’s “Adding a loading message” entry on Nov 15th, 2008.
May 7, 2009 at 10:18 AM
I’m having a hard time trying to implement this function http://www.americanfinancing.net/email-image-upload.cfm into the form. Does anyone have experience with this?
May 8, 2009 at 12:27 PM
This form is great, and exactly what I was looking for.
However, I just noticed, that when someone attempts to use the search form in my sidebar, instead of searching, it submits the e-mail form (which then produces error messages because the e-mail fields weren’t filled out).
How do I keep it from conflicting with my search form submissions?
May 9, 2009 at 12:35 AM
Nevermind. Figured it out. Feeling really stupid.
May 11, 2009 at 2:43 AM
Hello, this for the updates all question you answer to this page..
I just have a basic question.. I have 2 forms in one page using this script. the problem, i think when I click to submit the other one. the error msg will show on the first form I have.
pls let me know what i need to do with this..
Thanks and more power!
May 11, 2009 at 3:04 AM
Hello one more thing. do you have a credit card validation and phone number function of this script? pls can i have it?? thanks a lot for the help!
May 11, 2009 at 4:57 AM
hi pls ignore my 1st question.. I just read the comment and I will download your latest file.
I think this is the only I need if this is possible..
how can add a credit card and phone number validation function of this script?
thx!
May 11, 2009 at 3:58 PM
Hey Gabriel, awesome script BUT I have one problem, can you help?
When I receive my email after entering the info, ONLY the “message” is in the body of the email and it appears in the subject line of the email…
Is it possible for the email to have this data…
Subject line in email inbox “A user at yoursite has sent an email”
Body info in email:
Name: users name
Email: users email addy
Subject subject user entered
Message: message user entered
is this possible with this script?
May 12, 2009 at 11:06 PM
Brian, consider checking the previous comments from this post and you will find the solution
August 24, 2009 at 4:20 AM
Hey Gabriel,
I did what was mentioned in the previous post, but what shows in the subject line of the email is this…
“A user at yoursite has sent an email.” BUT then it puts all of the answers in the subject line of the email too.
Thats what I can’t figure out, I just want it to show the line I said.
Also, I have 2 radio buttons, one is YES one is NO, can those be validated and data sent in the body of the email too?
Also, email validation? User types it once, then has to again otherwise it gives an error?
Sorry man, I’m not a programmer but am doing VERY well with this amazing form script, there are just a few things I can’t figure out.
Thanks much.
Brian
May 13, 2009 at 10:37 AM
Thanks for your great bit of code.
When I try to navigate away form the page where the form is displayed before entering any data, “This field is required” is displayed on one of the input boxes and the page remains displayed.
May 14, 2009 at 8:41 AM
Hey Gabriel,
Fantastic article, very insightful and well laid out. I have tried to implement this on my own website but unfortunately I never receive the email.. The form itself works perfectly, if the correct information isn’t present it doesn’t validate and if the correct information is present it hides the form and displays a success message, yet I never receive an email. I have tried the mail() function before and I know that my host supports it so I’m truly stumped on why it isn’t working. Perhaps you can find the problem?
Best regards,
Elliot
May 14, 2009 at 8:53 AM
Hey Gabriel,
I seemed to have solved the problem, sorry to waste your time!!
Unfortunately I still have one other problem. With my website, I dynamically load in content using jQuery and AJAX. The only problem with this, is when I load in the content, in this case the contact form, it’s loaded after the DOM has been initialized. To fix this on other pages (let’s say where I’m using lightbox) I simply reinitialized that code but with your code I’m not sure how to accomplish this. Any chance you can help?
Best regards,
Elliot
May 14, 2009 at 3:20 PM
My final post so as not to spam your forums
I figured out how to reinitialize the form and in the process may have further advanced your script, I’ll leave that judgement upto you. I’ve condensed your code into 2 files, 1 JS and 1 PHP. The code looks like this:
JS:
$(document).ready(function() {
$(“form”).live(“submit”, function() { var str = $(this).serialize(); $.ajax({
type: “POST”,
url: “scripts/contact.php”,
data: str,
success: function(msg) {
$(“#note”).ajaxComplete(function(event, request, settings){
if(msg == ‘OK’) {
result = ‘Your message has been submitted, thank you!’;
$(“#fields”).slideUp(500);
} else { result = msg; }
$(this).html(result);
});
}
}); return false; });
});
PHP:
<?php
/* Credits: Bit Repository */
define(“WEBMASTER_EMAIL”, ‘YOUR@EMAIL.COM’);
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post) {
function ValidateEmail($email) {
$regex = “([a-z0-9_.-]+)”. # name
“@”. # at
“([a-z0-9.-]+){2,255}”. # domain & possibly subdomains
“.”. # period
“([a-z]+){2,10}”; # domain extension
$eregi = eregi_replace($regex, ”, $email);
return empty($eregi) ? true : false;
}
$name = stripslashes($_POST['name']);
$email = $_POST['email'];
$message = stripslashes(htmlspecialchars($_POST['message']));
$error = ”;
if(!$name) { $error .= ‘Please provide a name you wish to be addressed by. ‘; }
if(!$email) { $error .= ‘Please enter an email address. ‘; }
if($email && !ValidateEmail($email)) { $error .= ‘Please enter a VALID email address. ‘; }
if(!$message || strlen($message) < 10) { $error .= “Please enter a message [10 characters min.]. “; }
if(!$error) {
$mail = mail(WEBMASTER_EMAIL, ‘[Contact @ebarer]‘, $message,
“From: “.$name.” \r\n”
.”Reply-To: “.$email.”\r\n”
.”X-Mailer: PHP/” . phpversion());
if($mail) { echo ‘OK’; }
} else { echo ”.$error.”; }
}
?>
Cheers,
Elliot
May 24, 2009 at 4:02 AM
Hi, is it possible to clear the form fields if successful as well as display the success message?
September 15, 2009 at 9:54 AM
Hi Christian,
There is a way to clear form fields if successful by simply doing:
$('#ajax-contact-form')[0].reset();In the If statement msg == ‘OK’
Then that should the clearing of the form values.
Hopes this helps
May 28, 2009 at 6:07 AM
Hi, is there a way to consolidate the contact.html and contact.php files into one page?
Im trying to make the form work all as one page if possible. (I’ve already put the config.php and functions.php code into contact.php)
Great form though either way!
Thanks,
Scott.
May 28, 2009 at 12:26 PM
Sorry, disregard the above, I now understand AJAX a bit better!
Lol.
-Scott.
June 1, 2009 at 7:27 AM
Hi,
Can anyone tell me what to do to add a checkbox that if checked to send a copy of own message to the email user enters?
Thank you.
June 1, 2009 at 10:28 PM
Hello. Any clue as to why my email sends but the “Thank you” message does not? Its just showing the form.
June 12, 2009 at 2:22 PM
Thanks! Great Job!
June 18, 2009 at 3:38 PM
Hi,
I always get an alert saying “success” everytime i click the submit button.
What am i doing wrong??
Pls help
June 18, 2009 at 3:44 PM
I figured that out. I didnt up the jquery-1.2.6.min.js.
I would like the form not dissapearing.Just the message coming in and after sometime it fading away
How can we do it?
Thanks
Prady
July 23, 2009 at 12:55 PM
Hi,
Thanks for this useful tutorial, the only problem I am running into is validating XHTML 1.0 Transitional below is the error I get:
document type does not allow element “div” here
result = 'Your message has been sent. Thank you!</dThe element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements — such as a "style" element in the "body" section instead of inside "head" — or two elements that overlap (which is not allowed).
Is there any way to get around this?
Thanks again!
July 23, 2009 at 2:10 PM
@ned, thanks for your reply. To get around this, just put the javascript code in an external file. That validator (I think you used http://validator.w3.org/) did not strip the javascript code and detected that DIV inside the HEAD section of the page.
July 23, 2009 at 1:20 PM
[...] causing error in validation Hi, I used this tutorial to develop the contact form you can see on the left side bar here. Everything is working great [...]
July 23, 2009 at 3:12 PM
Thanks you for the reply, I seem to have a larger problem at the moment where I believe it is conflicting with another script I have on the same page.
If you look here : http://www.talkpaths.com/UP/index2.html
you see there is a rotating news section on the lower right. In this version I have not added the javascript to the document, but with all things the same, if you look here
http://www.talkpaths.com/UP/index.html
I have inserted the javascript and the image rotater is disabled.
I really need both pieces of code, any advice how to make them work at the same time?
Thanks again!
July 23, 2009 at 5:23 PM
actually I am having this same problem with all other js scripts I use on the same page as the form, it makes the other js functions null and void. for example, if you look here:
http://www.talkpaths.com/UP/full_service_work_JackieSkelly.html
there is a popout image viewer.
That same image viewer is implemented here:
http://www.talkpaths.com/UP/promotional_Lynx.html
but because I added the contact form to the side panel, the image viewer no longer woks.
Basically I really want to use the form I made following this tutorial but if I can not get it to play well with the other js snipits, then I have to start over and find another tutorial…
July 23, 2009 at 9:35 PM
@ned, follow these steps:
Find:
<form action="javascript:alert('success!');">and replace it with:
<form id="ajax-contact-form">In inline.js replace $(“form”) with $(“#ajax-contact-form”) and $(“form”).serialize(); with $(this).serialize();
I have seen that you are using MooTools. This library may be in conflict with JQuery. Consider checking the following URL: http://docs.jquery.com/Using_jQuery_with_Other_Libraries
Another thing that I have noticed is the fact that you include 2 JQuery libraries. Why? Only one is enough.
July 23, 2009 at 10:47 PM
@Gabriel,
Thanks again for your time and help. The fix you suggested did not work. I am assuming it is conflicting with mootools so I will try implementing a fix from that link you sent me.
Just to make sure you were looking at the right document:
http://www.talkpaths.com/UP/index.html
Am I still using two jquery libraries? If so can you tell me their names?
July 24, 2009 at 11:20 AM
@ned, in http://www.talkpaths.com/UP/full_service_work_JackieSkelly.html you use 2 JQuery libraries (ajax_contact_form/jquery-1.2.6.min.js and js/jquery.js).
July 28, 2009 at 8:01 AM
this code is fine. can you show me,when click link ajax should be happened.can you give me some example like this
August 2, 2009 at 6:38 AM
hi there
very nice code i was wondering is there a way to attach file to the e-mail message?
thanx
August 3, 2009 at 3:13 PM
@vjive, it can be added but not in AJAX. Many applications that you see out there are actually using IFRAMES to upload files. They just imitate the AJAX process.
August 3, 2009 at 6:43 AM
Does this not work in IE6. Even your demo page doesn’t work in IE6. You can’t type in the input fields. Thanks for the tut it came in very handy.
August 3, 2009 at 3:14 PM
@Kenny, I have just tested the form in IE6 (it’s not the first time) and it really works 100%. Maybe you are having problems with your IE.
August 6, 2009 at 8:51 PM
I’m trying to re purpose this for a more advanced form. I was wondering how I could validate the name field on a function. I tried a few things with eregi_replace and I seem to be missing something.
Thanks
August 7, 2009 at 9:11 AM
@bingeboy, How do you want to validate the username? Most of the accepted usernames in form fields should contain letters, numbers, periods and underscores.
August 11, 2009 at 7:06 AM
Works perfectly , but i can’t get the correct information from ajax form. Some characters (çşüöğı) are NOT viewing fine. For example:göktürk , ÅŸendoÄŸan. How I can fix that?
August 12, 2009 at 5:32 AM
[...] ENLACE [...]
August 22, 2009 at 10:48 AM
HI,
Got the form working great thanks.
Keeps going to my spam folders though which is a shame
A workaround for this would be to set the From: in contact.php to a designated name such as “Contact Form Submission from http://www.yourdomain.com”
But i have had no luck changing the contact.php for this, because when i do i get a syntax error on line 54.
Any ideas??
Thanks
September 1, 2009 at 2:20 PM
Hi! Great stuff and thanks for the hard work from all on this. I’m just wondering if anyone has had a problem with their “reply to” code in the config file? When I reply to one of the emails that I recieve using your code, it sends it to my email address as this is what is said to be done in the instructions. If I leave blank, it sends it your_domain.com…it sends it whatever I have in the config file rather than to the actual email account the message has been sent from. So, as a designer I’m unsure how to get the variable in there that is required to make this work properly. Any help much appreciated!
Bodhi
October 19, 2009 at 4:52 AM
You’re right, I got the same issue. There are a couple of errors in the code.
This is how I fixed it. This is an excerpt of the contact.php file, I’ve modified the reply to passage:
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
“From: “.$email.”\r\n”
.”Reply-To: “.$email.”\r\n”
.”X-Mailer: PHP/” . phpversion());
Also, I’ve removed the reply-to stuff in the config.php
hope it helps
September 2, 2009 at 2:37 AM
Hi,
Thanks for this script, it’s great.
I’m reworking my site at the moment and this has been a big help.
I ran into an issue around what happens when the user has JS disabled.
So I started working on a solution which was to hide the form if js was disabled and display an email link.
But I found a solution here – http://www.webmasterworld.com/javascript/3599878.htm
Using jquery I added this code to contact.html
$(document).ready(function(){
$("body").addClass("has-js");
});
fieldset {
display: none;
}
.has-js fieldset {
display: block;
}
.has-js p.use-email {
display: none;
}
Our contact form requires JS.....
I’m not sure if this is the best way to do this, but it gets the job done.
Hope this helps someone.
John
September 8, 2009 at 7:12 AM
Hey, you did a nice work here. Hope you can understand me, my english is very bad.
I’m trying to give a format to the mail you receive, but i’m not very sure where i can do that.
Ok, thanks
September 29, 2009 at 8:57 AM
Hi!
I realy liked this form, im trying to add some more fields to it, but whit no succes. Can you pleace give me a hint.
Here is the code im trying to work whit
Namn
E-post
Subject
Adress
Postnummer
Ort
Har du något medelande till oss?
In the contact.php i have added the new variables
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$adress = stripslashes($_POST['adress']);
$postnr = stripslashes($_POST['postnr']);
$ort = stripslashes($_POST['ort']);
and included them in the string
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.';
}
if(!$adress)
{
$error .= "Please enter your message. It should have at least 15 characters.";
}
if(!$postnr)
{
$error .= "Please enter your message. It should have at least 15 characters.";
}
if(!$ort)
{
$error .= "Please enter your message. It should have at least 15 characters.";
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.';
}
// Check message (length)
if(!$message || strlen($message) < 5)
{
$error .= "Please enter your message. It should have at least 15 characters.";
}
But its here i think the failure comes along, how do i do to get the new variables to be sendt whit the other information.
I have tryed this but whit no success
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $ort, $postnr, $message, $adress,
"From: ".$email."rn"
."Reply-To: ".REPLY_TO."rn"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo ''.$error.'';
}
}
Any tips to whats wrong?
Sorry for my bad english.
October 6, 2009 at 10:54 PM
What command does the form use to send the email?
I have downloaded the latest version but nothing happens when you submit the form.
October 6, 2009 at 11:02 PM
The form just sends the values to the PHP file (contact.php) that processes them and sends the message using the mail() function.
October 7, 2009 at 12:50 AM
It’s seams to work on my Mac development computer but on my windows web server it does nothing when the form is submitted.
October 11, 2009 at 8:07 PM
I am new to JS and AJAX, I am trying to implement this form but when I am trying to put the real data I am not receiving any email but if I am putting some test data e.g. test@test.com I am getting the email.Can someone help me?
Thanks..
October 14, 2009 at 4:53 AM
i get the following error Deprecated: Function eregi_replace() is deprecated in php 5
Anyone know how to fix this?
October 20, 2009 at 3:35 PM
Hey,
I appreciate your effort building, posting, and replying.. but I’ve encountered an error (and errors have come up on all ajax/php scripts I’ve tried..
whether or not I enter information in the forms and click submit, I get the following error
‘.$error.”; } } ?>
just below the Contact title on the demo page.
I had a feeling it was to do with testing locally, but I’ve uploaded the demo exactly and it still has the error.
I’d appreciate any info that could help me out. Cheers, Alex.
October 20, 2009 at 6:02 PM
Alex,
What you have encountered is not an error. You need to put the files in an environment where PHP is enabled. This could be your localhost or a website. You are not the first that tells me about this presumed “error”. In my post’s title and description I have clearly specify about PHP. I understand that you are a beginner that needs assistance. Also, I have reasons to believe that you encountered the same “problem” in other scripts as well (as you mentioned in your comment).
Gabe
November 5, 2009 at 5:06 PM
hi there, thanks for the nice work.
I’m wondering, how can I add a “please wait” message or a progress bar image right after the user click the submit button?
thanks
November 13, 2009 at 10:44 AM
Hi! Great stuff this form!
I really need help here, i’m building a form that have multiple checkboxes and don’t find the way to process that important data. I search a lot in differents webs and i try differents methods but nothing works to me.
I have to say also that i’m not a php, jquery or ajax expert but also i’m not a lazy person, i really search a lot but nothing works…
So if you can help me to process the multiples checkboxes I’ll be very grateful.
Thnks a lot ando sorry for my bad english.
November 19, 2009 at 11:55 AM
The only problem I have is that I don’t receive an email with the form data… Is there anything that I am missing in my code?
Who can debug my code granted I post it or email it to someone??
November 29, 2009 at 6:54 AM
[...] I implemented a language switcher. I used this jquery plugin for the photo gallery. I used the ajax contact form here. [...]
November 30, 2009 at 6:13 PM
Thanks for the script,is there any demo?Found something similar http://www.phpstring.co.cc/ajax-registration-module/
December 2, 2009 at 8:26 AM
Your script its great but, is there any tip to prevent the spam?
Tks in advance
December 3, 2009 at 6:01 AM
After 3 hrs i did it!
In contact.php insert:
//Anti-Spam
$postspam = (!isset($_POST)) ? true : true;
if($postspam)
{
$human = stripslashes($_POST['human']);
$error = '';
//Human
if (!$human)
{
$error .= 'SPAM.';
}
else
{
echo ''.$error.'';
}
}
// End of Anti Spam
Hope it helps to someone!
December 4, 2009 at 1:32 AM
This is not a way to prevent SPAM. It’s just an additional POST data that can be easily submitted by any robot.
December 5, 2009 at 4:00 AM
=( Any ideas in order to prevent the Spam?
December 4, 2009 at 1:43 AM
I’ve used this script with excellent results in the past.
This time I’ve tried to include it inside an “ajax content” popup based on ThickBox 3.1. (http://jquery.com/demo/thickbox/).
The problem is that when loaded in the ajax popup the form validation doesn’t work and also, the email is not being sent.
The “Success!” messages displays, and if I remove that event from the FORM tag, the form still doesn’t do anything.
Any hints on how to get this going?
For now I’ve left the form inside the html page and it works great.
Would love to have it inside the LightBox popup.
December 8, 2009 at 8:43 AM
It would be interesting if there is a way that on the “thank you message” to list an URL depending on which drop-down option was chosen from the form.
Is there a way to patch feedback.js so that a specific URL appears on the “thank you message” if the OPTION “GREENWAY” has been chosen from the SELECT “solution_provider”, but a different URL appears on the “thank you message” if the OPTION “ORACLE” has been chosen?
Would assume the change should be made inside:
if(msg == ‘OK’) // Message Sent? Show the ‘Thank You’ message and hide the form
{
result = ‘Your request has been received. Thank you!’;
$(“#fields”).hide();
$(‘#ajax-contact-form’)[0].reset();
}
Any hints?
Thank you!
December 13, 2009 at 3:15 AM
Social comments and analytics for this post…
This post was mentioned on Twitter by maheshvnit: RT @tweetmeme @bitrepository A simple AJAX Contact Form with JQuery and PHP Validation http://bit.ly/14dWKZ...
December 13, 2009 at 3:48 PM
Has anyone encountered any problems with Yahoo! Small Business hosting and this form? It works great on my GoDaddy Hosted site, but on my client’s Yahoo hosted site, it doesn’t work. Thoughts? Thanks!
January 3, 2010 at 7:42 AM
Implemented your form. Any idea why under ie8 (only ie8) it prints an alert box that says “success”? anyway to make it work in ie8 like it does in other browsers?
January 3, 2010 at 7:59 AM
Most probably you didn’t setup the script correctly. That alert is actually the response returned in case you do not have JavaScript enabled OR you have modified the form and the JQuery code doesn’t have any effect when you press the submit button. It is just a dummy ‘action’. I will update the form tag anyway because it creates confusion. If your problem persist and you need help installing the form on your website I can help you for a small fee.
January 3, 2010 at 8:21 AM
I got it work fro IE8… basically all I did was update Jquery min to be 1.3.2 and it seemed to cure my issue.
Thanks for taking the time.
January 5, 2010 at 3:07 AM
Hello
when I uploaded your premium script to my www folder, the page loads and at the top I get the error =
Notice: Undefined index: http://photoagahi.com/ in C:\wamp\www\contact.php on line 2
Notice: Undefined index: http://photoagahi.com/ in C:\wamp\www\contact.php on line 2
how can I fix it please
Hamed
January 5, 2010 at 3:33 AM
It may be something regarding the configuration of PHP on your server. However, I have updated the script. Consider re-downloading it. If that problem persist and you need help getting rid of it, I will help you to setup the form on your website, free of charge
January 11, 2010 at 2:16 AM
Hi mate, nice work,
I need your help to understand why all the email that I receive through your form are coming without Apostrophises.
Lots users are Italian and they used to use lots Apostrophises.
Any ideas? Does depends from the DOCTYPE?
Please help.
Thanks,
D
January 11, 2010 at 9:54 AM
The DOCTYPE has nothing to do with it. I made some test and the apostrophes are showing fine. Keep testing! Try to test the script in other hosts too.
January 11, 2010 at 9:39 PM
Just bought Premium script: I have added my email address to relevant scripts and uploaded all files to my PHP server. The form is displaying correctly and CAPTCHA image is showing, but when I type the CAPTCHA code it deosn’t validate. Nothing happens… I refresh and a new code appears and I type it but still nothing happens. The form cannot be submitted as is. I require captcha. Any idea? [PHP 5.2.0 with GD library enabled.]
January 11, 2010 at 9:45 PM
Please send me the URL to the contact form at support ( @ ) bitrepository { dot } com. I need to make some tests too. I am not sure how you tested it and what browser and OS you’re using. Perhaps you didn’t setup the script correctly or there is something related to your PHP configuration. We’ll fix this
January 12, 2010 at 10:03 AM
In case anyone else has this problem, it was a typo in the script:
In the realtime.validator.js, on line 153, you will see $.ajax. Change it to $j.ajax.
Thanks Gabe for spotting this.
January 13, 2010 at 1:50 AM
Hi Gabriel,
I was playing with your demo and when I disabled javascript in the browser the validation doesn’t work.
I am planning to buy the premium version. Will I have php validation in place in that version?(in case the Js is disabled)
January 13, 2010 at 4:53 AM
The real time validation can’t work without JavaScript enabled. However, the fields are validated in PHP to prevent SPAM and the script sends the mail only if the required fields are filled correctly. I will add a feature that will gracefully degrade the application for users with JS disabled and notify you when the script will be updated
January 14, 2010 at 7:41 AM
I loaded the form inside a facebox, it works fine with IE and Firefox, but not in Safari I keep getting a pop-up window with a “success!” displayed. when I click on submit…
Any ideas on how to fix it?
January 28, 2010 at 2:25 PM
its look cool and sound. but what about the SEO feature.url are not changed
February 4, 2010 at 4:44 AM
Hi there! This is a really nice form, I’m interested in purchasing the premium version. I’ve tested it and it works, everything nice n neat! One small problem though:
I need it for a Greek website. When I send a Greek message and receive it in my inbox, Thunderbird does not “realize” that this is in UTF-8, so, the message appears with “jibberish” at start. When I set the encoding afterwards, it’s readable. How can I “tell” the recipient that “hey, this is a UTF-8 message”. Can I set it somehow, and where?
Thanks a lot, good work and looking forward to hearing from you
Dimitris
February 4, 2010 at 6:20 AM
Hello Dimitris,
You need to edit the code where the
mail()function is used. The UTF-8 charset must be added to the mail headers. Here’s a URL that can help you with this endeavor:http://www.php.net/manual/en/function.mail.php#92976
PS: You can use
mail()to send HTML mails too. You just need to add the right headersGabe
February 22, 2010 at 8:58 AM
i get this error: {“sender_name”:1,”sender_email_none”:1,”sender_subject”:1,”sender_message”:1,”security_code”:1,”status”:1} what could be causing it!?
February 22, 2010 at 9:03 AM
This is not an error. It is the output resulted from parse.php. The form validation triggers after DOM is ready (all page elements are loaded). You probably clicked “Submit” before the page loaded completely. I will think of a solution for this too.
February 26, 2010 at 11:26 PM
I take this error too. I use jquery 1.4.2 when I return to 1.3.2 thats working again.
February 28, 2010 at 4:13 AM
Hi,
I put my yahoo id in the below area,
define(“WEBMASTER_EMAIL”, ‘yourname@yourdomain.com’);
Im testing this script with XAMPP locally, but after hitting the submit button, it is throwing an error,
The mail cannot be sent due to an internal error. Please retry later!
Pl. can u help me?
Arun
March 2, 2010 at 10:19 AM
Hi Gabe,
Great script, just a quick note. phpmailer isnt liking PHP 5.3.
Is giving me an error on line 470 split().
Have tried using preg_split() but no luck.
I think ts fine with previous versions of PHP.
Let me know if you find a solution.
Cheers
March 2, 2010 at 5:39 PM
Great script. Still trying to learn all that it does.
Is there a way to make the confirmation email (auto_responder) not show the reply-to email address that the form is set to send to?
like I want to set the reply-to email in the confirmation email to something like no-reply@domain.com rather than the email the form is sending the message to.
Any thoughts? Thank you for your time.
Regards,
Stan
July 24, 2009 at 1:22 PM
@Gabriel,
yes I guess I do. No clue how to fix that at the moment though. Could it cause problems?
I think I need to give up on getting your script to work with mootools as well…
Thanks for taking the time to check that out for me, and for the contact form tutorial. I am sure I will use it on future projects, and hopefully not screw it up as bad
July 24, 2009 at 3:55 PM
@ned, I doubt that the usage of both libraries can be the cause of the problem. I remember I have mistakenly used 2 in the same page and the scripts were working fine. If you agree, I can fix the problems you have on the site for a small fee. You can contact me via email (the address is on the site).
July 24, 2009 at 4:50 PM
@Gabriel,
I sent an email to support.
If there is a different address, I couldn’t find it.
January 14, 2010 at 11:14 AM
how can I validate more than 5 fields?.I only see 5 out of 8 validation fields on my form.
Can you please tell me what I am doing wrong?
January 28, 2010 at 9:16 AM
The premium contact form has just 5 fields. To add more, you just need to edit the application (at least the HTML page and the PHP page that parses the data). Please contact me at support ( @ ) bitrepository ( [dot] ) com if you need further assistance.