Talk to me about anything. If you’d like to work with me, or
even if you just need a hug, I’ll get back to you shortly.

Please enter your name

Please enter a valid e-mail

It's cold!

Say something!

jQuery AJAX Validation Contact Form with Modal + Slide-in Transition

Due to popular demand, here is a tutorial on how I created one of the more complicated pieces of machinery on my new site: the contact form. A lot of different techniques went into this, and I have a few people/places to thank for some of the original code that inspired my final product: primarily Design Shack for their tutorial on creating a slide-in contact form with ajax, Zachstronaut for his code on scrollable same page links (used all over my site, but most effectively on the contact link in my footer), and Yens Design for a quick how-to on creating the modal pop-up background darkening effect (surprisingly extremely easy to do with jQuery).

All you need is jQuery. No plugins are necessary for this to work, and it is only 2kb of extra code in addition to the jQuery library. This also works on all browsers, IE6 and up.

For a demo of what we are creating you need not go any further than the contact form at the top of this website, as well as the contact link in the footer, or you can go here. I’ve packaged all the files for your easy editing and applying to your own personal needs (please just don’t reuse my images. . . that would be a bit arse-like of you). I did include all of the same styling that I used on this form, to make it easier for me to write this tutorial and just in-case someone wanted to peak into how I pulled off some of the CSS.

The HTML:

<div class="container">
  <div id="contactFormContainer">
    <div id="contactForm">
      <div class="loader">&nbsp;</div>
      <div class="bar">&nbsp;</div>
      <form name="cform" class="contactForm" action="mail.php" method="post">
        <p>Talk to me about anything. If you&rsquo;d like to work with me, or <br />
          even if you just need a hug, I&rsquo;ll get back to you shortly.</p>
        <div class="input_boxes">
          <p>
            <label for="name">Name</label>
            <span class="name-missing">Please enter your name</span><br />
            <input type="text" value="" id="name" name="name" />
          </p>
          <p>
            <label for="e-mail">E-mail</label>
            <span class="email-missing">Please enter a valid e-mail</span><br />
            <input type="text" value="" id="e-mail" name="email" />
          </p>
          <p>
            <label for="message">Message</label>
            <span class="message-missing">Say something!</span><br />
            <textarea cols="" rows="" id="message" name="message"></textarea>
          </p>
        </div>
        <input type="submit" onfocus="this.blur()" value="Submit Form" name="submit" class="submit" />
      </form>
    </div>
    <div class="contact">&nbsp;</div>
  </div>
</div>
<div id="backgroundPopup">&nbsp;</div>

I’m going to assume you know what you are doing with HTML. If you don’t, well then. . . this is NOT the post to start with! Moving on. . .

The PHP:

    <?php
    //declare our variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = nl2br($_POST['message']);
    //get todays date
    $todayis = date("l, F j, Y, g:i a") ;
    //set a title for the message
    $subject = "Message from Your Website";
    $body = "From $name, nn$message";
    $headers = 'From: '.$email.'' . "rn" .
        'Reply-To: '.$email.'' . "rn" .
        'X-Mailer: PHP/' . phpversion();
    
    //put your email address here
    mail("youremail@yourdomain.com", $subject, $body, $headers);
    ?>
    <!--Display a thankyou message in the callback -->
    <div id="mail_response">
        <h3>Thank you <?php echo $name ?>!</h3><br />
        <p>I will answer your message soon as possible.</p><br />
        <h5>Message sent on: </h5>
        <p><?php echo $todayis ?></p>
	</div>   
Our mail.php breakdown:
  • The comments on this more or less speak for themselves. First we are getting the variables that are passed to the file from the javascript (NOT the HTML, that’s why the ID’s of the inputs don’t match up. I had to change email to e-mail so that it didn’t conflict with the comment forms on the blog posts). Also, the function nl2br() helps to replace any new lines that the user enters onto the ‘message’ textarea with line breaks so that you get a properly formatted e-mail.
  • Next, we define variables for the date, subject, body, and headers for the standard PHP mail() function.
  • After the mail() function is finished executing, you will see in our javascript that we will replace the form & loader with #mail_response so that the user gets some comfy feedback that we got their message! I see way too many folks leave out user confirmation on their contact forms, and that is just plain silly. Don’t leave your users in the dark!
  • I would also recommend putting some form of PHP spam protection in here as well. Another post, another time perhaps.

The CSS:

 
.container {
	width:960px; 
	margin:0px auto; 
	position:relative;
	}
	
/* Positions the contact form so it doesn't interfere with any other content, as well as a z-index above any other elements on the page */	
#contactFormContainer {
	position:absolute;
	left:368px;
	z-index:12;
	}
	
/* Hides the whole contact form until needed */	
#contactForm {
	height:289px;width:558px;
	background:#515151 url(../images/birdy.jpg) no-repeat 241px 11px; 
	border:1px solid #929191;
	display:none;
	padding:7px 12px; 
	color:#fff
	}   

/* Loading bar that will appear while the ajax magic is happening */
.bar{
	display:none; 
	background:url(../images/ajax-loader.gif) no-repeat center; 
	margin-top:100px; 
	height:40px; width:230px;
	}

/* This hides the form validation alert messages until needed */
#contactForm span { 
	display:none; 
	font-size:9px; 
	line-height:10px; 
	padding-left:6px; 
	color:#f5c478;
	}
	
/* Some styling for the contact button */
#contactFormContainer .contact {
	height:47px; width:211px;
	background:url(../images/contact_me.png); 
	position:absolute; 
	left:368px; bottom:-44px; 
	cursor:pointer;
	}
			
/* Hides the darkening layer for the Modal effect. The z-index is necessary for layering purposes, and be sure to keep the positioning/height/width the same */	
#backgroundPopup{
	display:none; 
	position:fixed; 
	_position:absolute; 
	height:100%; width:100%; 
	top:0; left:0; 
	background:#000; 
	z-index:11;
	}     

I did not include the CSS styling that I used for appearances, only what was necessary to get this functional. For everything that I used to create the form, please download the source files.

And our CSS rundown / explanation:
  • .container is just used for positioning everything in the middle of the page, and the position:relative property lets us absolute position the elements inside of the div.
  •  
    #contactFormContainer {
    	position:absolute;
    	left:368px;
    	z-index:12;
    	}
    #contactFormContainer is absolute positioning the whole contact form, this div is also necessary so that the .contact button moves with the contact form, and the z-index puts it above the darken div.
  • #contactForm Contains the form as well as all other content inside of it (loading bar, background, etc.) and is hidden until the .contact button is pressed.
  • The spans are hidden until a user tries to submit the form without properly filling out all of the fields. You will see why each one has its own class when we take a look at the scripting.
  • #backgroundPopup{
    	display:none; 
    	position:fixed; 
    	_position:absolute; 
    	height:100%; width:100%; 
    	top:0; left:0; 
    	background:#000; 
    	z-index:11;
    	}    
    #backgroundPopup is placed at the bottom of the page in the HTML (it needs to be OUTSIDE of any container or else it won’t work right in IE6). This is the div that will appear and have its opacity changed when the .contact button is pressed. Make sure its z-index is above everything, but below the #contactFormContainer.

 

The Javascript:

PLEASE NOTE: The following javascript is a little clunky and not very well optimized. For a much cleaner version of the validation portion of this script, please visit my post The Simple, Quick, and Small jQuery HTML Form Validation Solution.

$(document).ready(function(){
	//function for contact form dropdown
	function contact() {
		if ($("#contactForm").is(":hidden")){
			$("#contactForm").slideDown("slow");
			$("#backgroundPopup").css({"opacity": "0.7"});
			$("#backgroundPopup").fadeIn("slow"); 
		}
		else{
			$("#contactForm").slideUp("slow");
			$("#backgroundPopup").fadeOut("slow");  
		}
	}
	 
	//run contact form when any contact link is clicked
	$(".contact").click(function(){contact()});
	
	//animation for same page links #
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($(this.hash).length) {
				$(this).click(function(event) {
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();			   
					$('html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				});
			}
		}
	});



   //submission scripts
  $('.contactForm').submit( function(){
		//statements to validate the form	
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('e-mail');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.cform.name.value == "") {
			$('.name-missing').show();
		} else {$('.name-missing').hide();}	
		if (document.cform.message.value == "") {
			$('.message-missing').show();
		} else {$('.message-missing').hide();}		
		if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.message.value == "")){
			return false;
		} 
		
		if ((document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.message.value != "")) {
			//hide the form
			$('.contactForm').hide();
		
			//show the loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//send the ajax request
			$.post('mail.php',{name:$('#name').val(),
							  email:$('#e-mail').val(),
							  message:$('#message').val()},
		
			//return the data
			function(data){
			  //hide the graphic
			  $('.bar').css({display:'none'});
			  $('.loader').append(data);
			});
			
			//waits 2000, then closes the form and fades out
			setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);
			
			//stay on the page
			return false;
		} 
  });
	//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": document.documentElement.clientHeight 
	});  
}); 
Oh joy, lots and lots of javascript explainin’ to do:
  • $(document).ready(function() {}); is the necessary jQuery function required to kick off anything that runs after the page is loaded. All our code will be placed inside of this.
  • function contact() {
    		if ($("#contactForm").is(":hidden")){
    			$("#contactForm").slideDown("slow");
    			$("#backgroundPopup").css({"opacity": "0.7"});
    			$("#backgroundPopup").fadeIn("slow"); 
    		}
    		else{
    			$("#contactForm").slideUp("slow");
    			$("#backgroundPopup").fadeOut("slow");  
    		} 	
    Here we are defining the function contact that will run each time a link with the class .contact is pressed, using jQuery’s selectors:
    $(&amp;quot;.contact&amp;quot;).click(function(){contact()});
    This allows us to give that class to any link to execute the function that opens the contact form. What this function is doing is first determining whether the form is hidden or not, and if it is then it will slide the form down, and then change the opacity of the #backgroundPopup div as well as fade it in.
  • $('a[href*=#]').each(function() {
    		if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
    		&& location.hostname == this.hostname
    		&& this.hash.replace(/#/,'') ) {
    		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
    		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
    			if ($(this.hash).length) {
    				$(this).click(function(event) {
    					var targetOffset = $(this.hash).offset().top;
    					var target = this.hash;
    					event.preventDefault();			   
    					$('html, body').animate({scrollTop: targetOffset}, 500);
    					return false;
    				});
    			}
    		}
    	}); 
    This script grabs any anchor on the page with a same page link (e.g. #something), determines the distance between it and the destination, and then creates a scrolling transition. You can edit the speed at which it transitions by changing the ‘500′ on the line $(’html, body’).animate({scrollTop: targetOffset}, 500);.
  •   $('.contactForm').submit( function(){
    		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    		var email = document.getElementById('e-mail');
    		if (!filter.test(email.value)) {
    			$('.email-missing').show();
    		} else {$('.email-missing').hide();}
    		if (document.cform.name.value == "") {
    			$('.name-missing').show();
    		} else {$('.name-missing').hide();}	
    		if (document.cform.message.value == "") {
    			$('.message-missing').show();
    		} else {$('.message-missing').hide();}		
    		if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.message.value == "")){
    			return false;
    		} 
    I know there is a better way of doing this, but in the interest of time I quickly put this together. This is a nice long if statement that checks each required input field on the form to see if they are filled in properly or not. Doing it this way is definitely not a problem if you only have a couple of required fields, but if you have several than this will need to be rewritten. There are jQuery plugins out there that offer much more extensive validation functionality, but it was not necessary for what I wanted to accomplish with this simple form. If the field in question is not filled in correctly, then the corresponding error message (those spans we made earlier) is shown and return:false; stops the form from being submitted.
  •     
         if ((document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.message.value != "")) {
    			//hide the form
    			$('.contactForm').hide();
    		
    			//show the loading bar
    			$('.loader').append($('.bar'));
    			$('.bar').css({display:'block'});
    		
    			//send the ajax request
    			$.post('mail.php',{name:$('#name').val(),
    							  email:$('#e-mail').val(),
    							  message:$('#message').val()},
    		
    			//return the data
    			function(data){
    			  //hide the graphic
    			  $('.bar').css({display:'none'});
    			  $('.loader').append(data);
    			});
    			
    			//waits 2000, then closes the form and fades out
    			setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);
    			
    			//stay on the page
    			return false;
    		} 
      });
    If all of the fields are correct, then we run the script that hides the form, shows the loading bar until the ajax variable passing to the mail.php script is complete, and then set a timer that waits ‘2000′ until it closes the whole thing. If you’d like to demo this action, please do not use the form on my website unless you want to say something to me; you can demo the form that sends a message to nowhere here.
 

Keep in mind, this was one of my first forays into really screwing around with jQuery/javascript using some of my own code, so I’m sure things are not perfect, and a veteran may look at some of my scripting with a bit of “wtf?” on their face. If you do get that face, please let me know of a more optimal way of doing something, and I will definitely update the code and give you credit. If you have any questions, as usual please leave them in the comments here!

97 Comments
  1. Thanks for this one, it’s gunna be really useful on an upcoming project! Now we just need those swirls!

    June 14, 2009 at 1:07 pm
    Reply

  2. Great Technique - How broad do you think it will span cross browser? As much as I absolutely hate IE6 some of my clients still run on it.

    June 17, 2009 at 3:35 pm
    Reply

    • I’ve tested this tutorial on IE6-8, Firefox 3.0, Google Chrome, Opera 9.5, and Safari 3.2.1, which should just about cover it.

      June 17, 2009 at 3:44 pm
      Reply

  3. fantastic tutorial! I will definitely use it in future projects..I know it’s not much, but i put the link to your article on my blog..
    cheers!

    June 18, 2009 at 3:56 am
    Reply

  4. Great tutorial and a nice effect. When curious people click on the contact then change their mind it would be nice to be able to remove the form by clicking outside the area. Lots to learn here. Thanks!

    June 19, 2009 at 4:06 pm
    Reply

    • Good idea Matt, that would be easy enough to implement as well I think. Could probably just add an onclick to the div that darkens the background behind the contact form to do the trick.

      June 19, 2009 at 4:19 pm
      Reply

  5. What an amazing tutorial! Thank you very much for sharing :)

    June 19, 2009 at 4:38 pm
    Reply

  6. Warren

    Is it possible to clear the fields and/or reload a fresh, empty form when it’s hidden?

    June 19, 2009 at 8:45 pm
    Reply

    • Yes, it’s possible, you would just have to create a button with an onclick listener that sets the values of the input fields to nothing. I just decided not to include that because it’s only 3 fields for my form and that would be a bit unnecessary :)

      Otherwise if you needed to fill the form out a second time, like most websites you’d just refresh the page.

      June 20, 2009 at 4:18 am
      Reply

  7. I don’t see a link to the demo, is there a demo and I am just missing it?

    June 19, 2009 at 10:42 pm
    Reply

    • Yup, you’re missing it. This tutorial is how I created the contact form for this website - so just click on the contact button at the top. You can also view a stripped down version by downloading the demo files.

      June 20, 2009 at 4:20 am
      Reply

  8. Johnny

    Excellent script. However, I’m concerned with mail injection and spam bots. I’ve added an empty field to the script and a check to filter for bots that fill out every field, but I have no way of really testing it.

    Also, when tested with a comma in the name field, the mail was shown as sent, but I didn’t receive anything.

    June 21, 2009 at 1:49 am
    Reply

  9. Johnny

    Can’t seem to replicate the comma problem in subsequent testing. Weird.

    I purposefully didn’t fill out the email correctly, left name blank and message blank the first time around, pressed “Send message”. Of course, the script spit out the error messages. I then filled out the name with the “,” separating two words, left the message blank, email was kept in the incorrect form, so as expected, it didn’t send e-mail again and spit out the error messages. On the third pass, i filled everything correctly and the message was “sent”, but never received.

    June 21, 2009 at 1:59 am
    Reply

    • Well it sounds like it’s making its way to the mail.php script, so be sure to double check that file and see if everything is updated in there correctly. It’s a pretty simple mailer script, so there shouldn’t be much that could go wrong as long as the variables were all kept the same throughout and you changed it to go to your e-mail address.

      June 21, 2009 at 2:52 am
      Reply

  10. xxxevilgrinxxx

    this is wonderful, I can’t wait to give this a try :)

    June 21, 2009 at 8:42 am
    Reply

  11. Great info, nice blog

    June 21, 2009 at 5:01 pm
    Reply

  12. Thanks for the complements.

    And guys, seriously, stop using my contact form to test the php mail sending lol, I’m getting a bunch of e-mails with nothing but ‘asdf’ etc. for the subjects and body ;)

    Please use the demo files to try it out on your server, unless you are sending me a legit message!

    June 22, 2009 at 9:25 am
    Reply

  13. I’d recommend to anyone that plays around with jQuery to give this article by NETTUTS a read: 10 Ways to Instantly Increase Your jQuery Performance.

    It showed me that I definitely could have done a couple of things in the javascript for this form to improve performance time a bit.

    June 23, 2009 at 2:26 pm
    Reply

  14. can i use this with codeigniter?

    June 30, 2009 at 12:52 pm
    Reply

    • Yes, codeigniter is a PHP framework, so nothing extra would be required.

      June 30, 2009 at 12:59 pm
      Reply

  15. Dave

    Hi Joren
    Thanks for this great tut.
    Works like a charm and croos-browser. (thank god there is IE6…)
    I’ve got two question.

    1. The date format wich is sent by the form, hets the English date format. I tried allmost anything, whats possible for me as a php noob to get my local date format in Dutch. But nothing had any effect.
    Do you know a workaround how to get the Dutch date, both in mail form and the email that is sent.

    2. Is it possible to add some kind of validation (or how it’s called) that the sender receives a thank you message with a copy of the mail he had sent?

    I’m really a dork when it comes to forms, so i have no idea if it take a lot of work to get those things like i mentioned.
    Allthough, i like this form a lot and definately will use it in the near future.

    Thanks!

    regards
    Dave

    June 30, 2009 at 5:31 pm
    Reply

    • Hey Dave, not sure exactly what you would need to do, but if you take a look here http://us.php.net/manual/en/function.date.php that lists all of the formatting options to edit the string in the php date() function, and as far as I know that is all that is available.

      To answer your second question, that is an easy one. You would just need to modify the mail.php file to include a second php mail() function, so that it sends two e-mails when the script is ran through. All you would need to do is change the string within the function around a little bit, e.g. change the reply-to address to your own, and the ‘to’ field (which would be the first parameter of the string), and change around the subject and message etc. (you would need to create new variables for everything). For a further explanation on the mail() function, you can find that info in the same place at http://us2.php.net/manual/en/function.mail.php

      Hope that answers everything!

      June 30, 2009 at 6:41 pm
      Reply

      • Sravanthi

        Hi Joren
        Great demo.Its nice and easy.But i need to display 3 modalboxes in single page.Only one modalbox is working.please give me suggestion on this.

        July 1, 2009 at 5:14 am

  16. Sravanthi

    Iam a Ruby On Rails Devoloper

    July 1, 2009 at 5:15 am
    Reply

    • Welp Ruby on Rails won’t have anything to do with it except for replacing the mail.php file with rails scripting. If you are creating three modal boxes that have the exact same effects, then you need to create a jQuery function that pulls info from something else in the element so that it knows which item you are clicking on and what you want to appear. If you need a better explanation, I would suggest downloading some kind of photo gallery modal popup script like lightbox http://www.nickstakenburg.com/projects/lightview/ and playing around with it.

      July 1, 2009 at 9:00 am
      Reply

  17. ahmad

    Hi,

    I tried to use your marvelous contact us script. It worked very nicely in Firefox but when i used a flash banner header the sliding motion suffered a bit and it became jerky and not smooth.

    In IE 6.0 it didn’t worked at all when flash was there. same was case with the IE 8.0

    Any suggestions will be appreciated

    Thanks
    Ahmad

    July 5, 2009 at 2:07 pm
    Reply

    • Well the jerkiness is nothing wrong with the scripting. Rendering a web page is just like running any other application on a computer, if it’s too heavy of a load for the computer or the browser, then the FPS is going to drop. Either your computer isn’t very fast, or it sounds like you are just trying to accomplish too much on one web page.

      As far as it not working at all with IE6 or IE8, I can’t really help ya there unless you provide me with a link to look at.

      July 5, 2009 at 2:42 pm
      Reply

      • ahmad

        hi,

        Lots of Thanks for replying. I downloaded your contact form . its works very fine in IE 6 and Fire Fox but when i put the form container in my won page which has got a flash banner in it it satrts working badly otherwise as you have written on top your script works very nicely in all browasers :)

        Well its not overloaded

        It uses
        1. Flash banner=120kbs
        2. Background image: 18kb
        3. Two buttons (18kbs and 4 kbs)
        4. Html page size is 5kbs
        5. Two CSS sheets of some 50kbs

        I will mail you the site url in the contact form above.
        I hope it lands in your inbox

        Thanks

        Ahmad

        July 5, 2009 at 10:48 pm

      • ahmad

        I sent you the URL in contact form above

        Thanks in advance

        Ahmad

        July 5, 2009 at 10:50 pm

  18. Jenny

    Thanks alot, too beautiful work !

    July 11, 2009 at 7:21 pm
    Reply

  19. Hi joren,

    Thanks for the sharing the advantage of css with jquery to show case such a nice contact form.

    I would like to inquiry, instead of the “contact me” section is being standalone (with css and jquery), is it possible to integrate into of the page in Wordpress?

    For example, i create a page called - Contact. When visitor clicked on it, it will show the same lightbox effect and slide as well. Is it technically possible?

    Looking forward to hear from you soon. Thanks

    July 14, 2009 at 4:51 pm
    Reply

    • Yup, that should be completly possible. I’ll just try and point you in the right direction, since I don’t know exactly what you would need to do. The content of each wordpress ‘page’ is going to be stored in the database somewhere. What you would need to do is find a wordpress function that would let you call that data and place that function in your header somewhere so that it is on every page. Honestly though, the only benefit to doing it that way is if you had someone constantly updating the contact page, otherwise I would just follow the tutorial.

      July 14, 2009 at 6:36 pm
      Reply

  20. Jorge

    Nice tutorial, I need help with something. I like to have some check box in the form can some help me. I added in the form :

    test
    test

    I need to know who to pass the variable checked to the mail.php.

    thx in advance

    July 16, 2009 at 11:11 am
    Reply

  21. Thanks for tutorial. It was a useful to me in an application I am building.

    July 18, 2009 at 9:04 am
    Reply

  22. Hi,
    I am using tinymce editor for inline jquery editable functionality. After editing any text from editor and submitting ajax function tinymce is rendered with raw html.
    Please tell me how to stop displaying raw html in FireFox.
    Above project is not working in IE7.

    August 10, 2009 at 8:25 am
    Reply

    • Hey Surya,

      Sorry, but I’m a little lost by your question. I haven’t had any experience using TinyMCE other than with open source CMS’s that already had it running.

      August 10, 2009 at 8:30 am
      Reply

  23. Hi Joren,
    Your script is working fine. But the only problem is that when there is a flash animation, the contact form will opening behind the flash. please give some fix. ht tp: // 58.68.24.228/ga/shibani/index1.html

    August 19, 2009 at 6:11 am
    Reply

    • Hello Elyot,

      That happens with just about anything and flash. In the HTML, you have to set the flash embed to wmode=transparent. I can’t tell you exactly where to write that, because it depends on the method you are using to embed the flash file to the web page, but if you do a little Google search on that you should come up with what you’re looking for. If that doesn’t help, you can always try setting the rollover to a higher z-index with CSS, but the wmode attribute should fix your problem.

      August 19, 2009 at 8:47 am
      Reply

  24. gaston

    Hello… Im form Argentina, sorry for my regular english. one questios… in the PHP code, i dont understand too much but i need recieve the message in my mail and i dont no how to do… please help me. I like recieve the message in my mail “jgcoroleu@gmail”. what code i need?

    August 22, 2009 at 1:37 pm
    Reply

    • gaston

      its me again… looks my page http://www.aserraexport.com.ar/arcoroleu.html, you will see my contact form but dont works because i dont know to modify mail.php tha to recieve the messages in my mail “jgcoroleu@gmail.com”. please… can you tell me the code i need… thnx

      August 22, 2009 at 2:28 pm
      Reply

    • Hey Gaston,

      It looks like my post was a little screwed up, there was some code missing from the section that went over the PHP end of things. Take a look at it again, you should be able to tell now where to change the e-mail address. Sorry about that!

      August 23, 2009 at 12:46 pm
      Reply

  25. Jared

    Joren,
    Thanks for the code, I THINK I can make this work for what I’m doing. Just a question though, have you ever used this code with multiple elements on a site? Like for instance, I’m going to use it with a brief biography section with about 7 people or so. Each one will have a short bio and pic and email form. It’s going to be quite the challenge for me since I’m mostly a designer and not developer but I just wanted to know if it’s possible to do with this code or if you’ve ever done it. Also, if you have any resources I could look at to point me in the right direction that would be most helpful as well. Thanks in advance!

    September 1, 2009 at 10:58 am
    Reply

    • Hey Jared,

      That would not be hard at all, you would just have to create a separate function for each form that would pull out (so a new ID etc. for each one). The only issue is, that’s a lot of javascript animations to have running on a page, so it could end up pretty clunky, but you won’t know until you try it.

      September 1, 2009 at 11:18 am
      Reply

      • Jared

        Thanks for the quick reply! I think I may end up trying something else as I decided I really don’t need it to slide up and down but a popup would work fine for me. I will definitely keep this bookmarked for future reference though, thanks for posting!

        September 1, 2009 at 11:29 am

  26. Surya

    Hi,
    I need to parse Safari Bookmarks (Bookmarks.plist) file using asp.net
    Actually it is in Binary format which is tough to read.

    September 12, 2009 at 3:29 am
    Reply

  27. For anyone who is previously using this script, I just discovered a limitation with using GET as a method for passing the variable. It only allows for so many characters to be used in the textarea, so I had received a couple of e-mails with blank messages because they were too long. I updated the post and the source files to use the POST method.

    September 12, 2009 at 4:52 pm
    Reply

  28. Mike

    Hey Joren Great script!
    Works great..only problem I’m having is that on one of my pages I’m also running a simple jquery innerfade of some images. The contact form appears behind the innerfade.
    Kind of like what the user posted above about the Flash animation only this is just an innerfade of images. Any suggestions to get the contact form to appear in front of the innerfade?
    Thanks and keep up the great posts!

    September 24, 2009 at 11:20 am
    Reply

    • You’ve gotta play with the z-index’s of them. Internet Explorer’s handle z-index in a funky way, so check out this link for a better understanding of it (and be sure to read through the comments).

      http://www.brenelz.com/blog/2009/02/03/squish-the-internet-explorer-z-index-bug/

      September 24, 2009 at 11:33 am
      Reply

      • Mike

        Hey Joren,
        Worked great. Thanks for getting back so quick. Changed the z-index property and that did the trick. Now my only other new problem is that the contact form automatically closes only on the page where it is floating above my innerfade of images. Any ideas?

        September 24, 2009 at 8:33 pm

      • No problem. If you are using the exact same code line for line on two different pages, but the form is acting differently, then it is more than likely that you are getting interference from other javascripts on that page. For example, a variable name might be getting used twice on accident.

        I would use process of elimination. Just take out certain other items at a time until you get it to work, and then add some back until you narrow it down then make your changes.

        September 24, 2009 at 8:47 pm

      • Mike

        Hey Joren,
        I just got it going. I figured I would stick with the z-index property for a bit. I changed the z-index property on the #backgroundPopup to a higher value, 13, and that is now keeping the form from automatically sliding backup. I’ve tested in IE and Firefox so far.
        Z-index can be quite powerful.

        Thanks again for your timely responses and your posts.

        September 24, 2009 at 8:58 pm

  29. Steve T

    Hi Joren,

    Great script to add to my arsenal! Quick install, everything is working great. The only issue I’m facing is when I re-size my browser to test the script on my page, the form and tab are shifting rather than staying fixed. I know the script revolves around absolute positioning based on the 960px container.

    My question… is there any way to set the positioning so it remains consistent regardless of screen resolution?

    Thanks Joren,

    Steve T

    September 30, 2009 at 6:30 pm
    Reply

    • It sounds to me like you want to be trying fixed positioning as opposed to absolute? Or, if you have the website sitting flush with the left side instead of centered like in the example, just get rid of the “margin:0 auto” that is on the container div.

      October 1, 2009 at 8:09 am
      Reply

      • Steve T

        Hi Joren,
        Thanks for the reply.

        My site is a “centered” design @ 780px wide.

        Before contacting you, I already tried to use a “fixed” position rather than “absolute”… still the same result. I cant figure out for the life of me how to make this “fixed” on the upper left side of my page. I don’t want the tab to move every time the browser is re-sized. I’ve tried everything I keep coming up empty.

        Any additional thoughts?

        Thanks again for this great script, now if I can get this in a “fixed” position, I think we have a home run here.

        Steve T

        October 1, 2009 at 10:03 am

      • If you’d like to send me a link to the page that you are talking about, I’d be more than happy to help you. Just send a message through my contact form, thanks ;)

        October 1, 2009 at 10:13 am

  30. Brad

    Just want to take a moment to thank you for this tutorial. Great job!

    October 3, 2009 at 1:36 am
    Reply

  31. Steve T

    Joren,

    I emailed my link a few times, did you get it?

    Thanks,

    Steve T

    October 5, 2009 at 1:59 pm
    Reply

    • Sorry, no, I have not received anything. Maybe it went into my spam box, did you try my website’s contact form?

      October 5, 2009 at 3:33 pm
      Reply

      • Steve T

        Joren,

        Your contact form hangs when I try to send you my information. I just sent again. I am assuming you still haven’t received it. Please let me know.

        Thanks,

        Steve T

        October 7, 2009 at 1:30 pm

  32. Thanks for a great tutorial.
    One thing is confusing me with the slide-in animation.
    In firefox (ie7 works - for a change!) the form rolls down over my header, disappears under the content wrapper, and then reappears over it. Everything else is working fine!
    I placed the email form code right after the body tag.

    I havent been able to upload it yet. Any ideas what is causing this?

    October 9, 2009 at 9:37 am
    Reply

    • Hey Yan, sounds like a z-indexing issue to me. Make sure you play around with that. If you still have trouble, send me a link through my contact form and I’ll check it out for ya.

      October 9, 2009 at 10:58 am
      Reply

  33. Thanks for the walk through, very precise and the form works great. You have an awesome site btw!

    October 12, 2009 at 8:19 am
    Reply

  34. I am having a problem getting the email once someone submits the form. Everything seems to be working but I get no email when someone fills it out. I replaced the email with mine in the mail.php file. I do have to do anything else?

    Thanks for the help.

    Daniel

    October 21, 2009 at 6:50 am
    Reply

    • Hey Daniel, it’s a possibility your web host has an issue with the way the mail headers are setup, so you may need to play around with that or specify the PHP version. If you keep running into a wall I would probably give them a ring.

      Thanks for the complement John ;)

      October 21, 2009 at 8:09 am
      Reply

      • Thanks. I really do like the tutorial. Keep up the good work.

        October 21, 2009 at 9:28 am

      • danny

        Hi Joren, I’m having the same problem as Daniel, in that I don’t receive any mail. Where would I go About specifying the PHP version?

        thanks

        January 29, 2010 at 6:03 am

  35. ChrisJ

    This is exactly what I was looking for. Thank you very much for doing the work and making it available for everyone.
    Do you think that I could trigger the slide down with a link somewhere else on the page? The situation is that I have another Contact Us link a bit further down the page and would like to have it trigger the slide but so far I’ve not been able to do it.

    November 5, 2009 at 6:22 pm
    Reply

    • This line, as it says, tells the script to run the contact form function whenever a link with the class of “contact” is clicked ;)

      $(”.contact”).click(function(){contact()});

      So, to answer your question, yes, just give whatever link you want the class “contact” and you’ll be all set. You’ll see I also have it on the link in my website’s footer.

      November 5, 2009 at 6:41 pm
      Reply

      • ChrisJ

        Thanks Joren!
        I had tried that and it didn’t work, not sure where I messed it up. After reading your note I tried again and everything is fine.

        Thanks again.

        November 6, 2009 at 9:30 am

  36. james

    Something I have noticed is that if I play with this script on my server w/ sendmail it all works fine.

    However, if I use this tut on my server that runs msmtp inplace of sendmail… I get the form, it validates and also sends. However, after the loading animation it simply slides out without displaying any confirmation like in “mail_response.”

    Any idea what would cause this or a workaround?

    Thanks

    November 16, 2009 at 8:19 am
    Reply

    • If the script is sending the e-mail still then one of two things is happening. Either the AJAX isn’t getting the response it wants from your mail script telling it that it did send, or it’s just taking longer to process the mail script. There is a timer on this script that determines when the contact form goes back up, the setTimeout() function near the end of it. You can see it is currently set to 2000, which you can make higher and see if the latter is your issue. You may also need to add the response data (some html thank you message) so that the script has something to replace the loader div with.

      November 16, 2009 at 1:22 pm
      Reply

      • james

        Hrm, it works when i set it to 7000. Which seams little excessive to me. Is there a way to wait for a response from the mail server or the script before it skips to the div?

        Thanks

        Oh and great script btw.

        November 16, 2009 at 5:53 pm

      • Sadly, it’s probably taking so long because your mail script is much larger or your server is really slow. I don’t know of a way to delay a jQuery animation until it receives the data response, although I’m sure it’s out there. If anyone else is able to chime in on this, I’d be much obliged.

        November 18, 2009 at 4:31 pm

  37. Very nice and useful tutorials to web designers,
    Thanks for posting.

    December 21, 2009 at 12:20 am
    Reply

  38. cuchetti

    Hi, great tut. I have a question, where are you defining #mail_response. It’s not in the style sheet.

    December 22, 2009 at 2:09 pm
    Reply

    • Thanks! I’m not actually using it for any CSS in this example, I think that div was just left over from the code I used on this website, so you can either take it out or apply your own styling to it.

      December 22, 2009 at 2:20 pm
      Reply

      • cuchetti

        Hi Joren,

        One more question, I see the #messageSent defined in the style sheet, but it’s not in the code. How do you get the confirmation message to show up without it?

        December 23, 2009 at 2:17 pm

      • Hah looks like you found more leftover code from who knows what, sorry about that. Hiding the confirmation message isn’t necessary at all because we will never see the response from the PHP until the mail function is finished processing, since the PHP script does not get called until our AJAX request. I’ll take that bit out of there.

        December 24, 2009 at 11:39 am

  39. cuchetti

    Joren, Thanks for replying to my other questions. Hate to pester, but I’M SO CLOSE. Everything is working great, except once the form is sent, you can’t send it again. That would normally be fine, except I have an “contact” button in multiple divs on a jQuery ’slider’ type page (where it appears to be multiple pages, but it’s only one page and multiple divs). Is there anyway to reset the form, not just the form fields, but the whole thing, once it’s already sent?

    December 27, 2009 at 1:34 pm
    Reply

    • Sure thing, just apply the jQuery function .show() to the id you have set for the form after everything is processed.

      December 27, 2009 at 1:49 pm
      Reply

      • cuchetti

        you are a god!

        December 27, 2009 at 2:50 pm

  40. First i have to say great tut!
    But i have problems with the z-index.
    Every image within the container is not covered by the backgroundPopup.
    i tried to give evey div an z-index but it still won’t work.
    Any explanations?
    Thanks in advance

    January 2, 2010 at 5:50 am
    Reply

  41. Very good! I enjoyed it!

    January 13, 2010 at 1:10 pm
    Reply

  42. Richard

    Thank you… Thank you… Thank you… The mail.php was soooo easy to use, unlike others that I have found and your tutorial was easy to implement into my site. Again I thank you for your nice looking and easy to use contact form.

    Would post the link to my site but I am still developing it.
    Thanks
    Rich

    January 17, 2010 at 9:10 am
    Reply

  43. Mahmood

    Thanx for a great Tutorial,

    One Question though, how can we close the modal contact popup window with the ESC button.

    Thanx,

    February 7, 2010 at 3:31 pm
    Reply

    • Sure thing. You just have to add a new jQuery even handler that listens for that keypress, like so:

      $(”#contactForm”).keyup(function(event) {
      if (event.keycode == 27) {
      $(this).slideUp(”slow”);
      }
      });

      27 is the numeric value for the escape key. For a resource of these, check here http://unixpapa.com/js/key.html

      February 7, 2010 at 7:50 pm
      Reply

  44. [...] Visit Source. [...]

    June 17, 2009 at 6:55 am
    Reply

  45. [...] jQuery Validation Contact Form with Modal + Slide-in Transition [...]

    June 17, 2009 at 3:31 pm
    Reply

  46. [...] Joren Rapini: jQuery Validation Contact Form with Modal + Slide-in Transition [...]

    June 21, 2009 at 1:05 pm
    Reply

  47. [...] jQuery Ajax Validation Contact Form with Modal + Slide-in Transition Due to popular demand, here is a tutorial on how I created one of the more complicated pieces of machinery on my new site: the contact form. All you need is jQuery. No plugins are necessary for this to work, and it is only 2kb of extra code in addition to the jQuery library. This also works on all browsers, IE6 and up. [...]

    September 9, 2009 at 9:10 am
    Reply

  48. [...] jQuery Ajax Validation Contact Form with Modal + Slide-in Transition Due to popular demand, here is a tutorial on how I created one of the more complicated pieces of machinery on my new site: the contact form. All you need is jQuery. No plugins are necessary for this to work, and it is only 2kb of extra code in addition to the jQuery library. This also works on all browsers, IE6 and up. [...]

    September 24, 2009 at 8:13 pm
    Reply

  49. [...] The contact me link will darken the page and pull down a JavaScript powered box from the corner which looks very effective on a design such as this one. Joren has included instructions on how he did this on his Blog. [...]

    November 9, 2009 at 10:00 am
    Reply

  50. [...] jQuery AJAX Validation Contact Form with Modal + Slide-in Transition | The Blog of Joren Rapini [...]

    December 1, 2009 at 12:01 am
    Reply

Leave a Reply