3
This is it folks; the beast! This is the contact form to rule them all!
What we have here is a contact form structured with a nice marriage of XHTML and CSS, validated via jQuery, a dynamically generated captcha image for security, and PHP logic to handle the transfer of data. Not enough for you? Too bad.
Let’s do this…
We start out with a basic XHTML form structured with your run of the mill form elements:
<div id="style"> <form id="myform" name="myform" method="post" action="" onsubmit="return checkform()"> <div id="group1"> <label>Name <span class="small">Please tell us your name</span></label> <input type="text" name="name" id="name" onkeydown="inputCounter(this.form.name,this.form.remName,30);" onkeyup="textCounter(this.form.name,this.form.remName,30);" class="text-input"/> <label class="error" for="name" id="name_error">You must provide your name!</label> <label>Email <span class="small">Please provide your email</span></label> <input type="text" name="email" id="email" onkeydown="inputCounter(this.form.email,this.form.remEmail,30);" onkeyup="textCounter(this.form.email,this.form.remEmail,30);" class="text-input"/> <label class="error" for="email" id="email_error">Please provide a valid email!</label> <label>Message <span class="small">Please provide a detailed message</span></label> <textarea name="message" id="message" cols="60" rows="5" onkeydown="textCounter(this.form.message,this.form.remLen,512);" onkeyup="textCounter(this.form.message,this.form.remLen,512);" class="text-input"></textarea> <label class="error" for="message" id="message_error">What, no message?</label> <div id="captcha"> <p><img src="/Scripts/captcha.php" /></p> <label>Security Code <span class="small">Enter code from image</span></label> <input type="text" name="code" id="code" onkeydown="inputCounter(this.form.code,this.form.remName,5);" onkeyup="textCounter(this.form.code,this.form.remName,5);" class="text-input"/> <label class="error" for="code" id="code_error">Please provide the code from the image!</label> <label class="error" for="code" id="wrong_code">Wrong Code! Please try again!</label> </div> </div> <p></p> <div class="buttons"> <button TYPE=reset NAME="resetButton" VALUE="Reset" class="button">Clear</button> <button TYPE=submit NAME="submitbutton" VALUE="Submit" class="button">Submit</button> </div> <div class="spacer"></div> </form> </div>
The structure is styled via external CSS:
#style label.error{ font-family: Geneva, Arial, Helvetica, sans-serif; font-weight:bold; font-size:10px; color:#FF0000; text-align:left; width:200px; height:15px; padding-left:20px; } label#name_error{background: transparent url(/images/exclamation.png) no-repeat scroll left;} label#email_error{background: transparent url(/images/exclamation.png) no-repeat scroll left;} label#message_error{background: transparent url(/images/exclamation.png) no-repeat scroll left;} label#code_error{background: transparent url(/images/exclamation.png) no-repeat scroll left;} label#wrong_code{background: transparent url(/images/exclamation.png) no-repeat scroll left;} #response{height:400px;} #response p{ color:#0066CC;font-weight:bold;margin:6px 0px; } #response p.reply{ color:#666666; font-size:115%; } #checkmark{ position:relative;top:0px;left:0px; } #style{ padding:20px; border:1px solid #666; } #style label{ display:block; font-weight:bold; text-align:left; width:180px; color:#000; } #style input{ font-size:12px; width:250px; margin-bottom: 10px; } #style .button{ margin-top:10px; width:80px; height:30px; text-align:center; line-height:31px; color:#000; font-size:11px; font-weight:bold; border:1px solid #666; } #style .small{ color:#000; display:block; font-size:12px; font-weight:normal; text-align:left; width:180px; }
When the contact form loads in the browser we have several things that take place. First and foremost we are using jQuery’s
$(document).ready(function() { "your magic here" });to perform several DOM manipulations. We use the aforementioned event to govern when the logic is performed, and in this case it all takes place when the DOM is finished loading, NOT the entire page; JUST the DOM.
Basically this little chunk of jQuery makes sure that the logic waits only until the document structure is fully parsed before it performs its rich behaviors. Traditionally one would use the old
window.onload = function(){ "Your magic here" };which executes the statements after the entire page has loaded in the browser, including all external resources such as images. The jQuery method is simply faster.
We have an external validate.js javascript file that is linked to the page in the header. Here is what takes place as soon as the DOM is fully parsed…
$(document) .ready(function() { $('.error').hide(); $('input.text-input').css({backgroundColor:"#FFFFFF"}); $('input.text-input').focus(function(){ $(this).css({backgroundColor:"#bfddf7"}); }); $('input.text-input').blur(function(){ $(this).css({backgroundColor:"#FFFFFF"}); }); $('textarea.text-input').css({backgroundColor:"#FFFFFF"}); $('textarea.text-input').focus(function(){ $(this).css({backgroundColor:"#bfddf7"}); }); $('textarea.text-input').blur(function(){ $(this).css({backgroundColor:"#FFFFFF"}); }); $("input#name").select().focus(); });
First jquery targets all elements with a class of “error” and hides them from the browser.
$('.error').hide();
These will pop up later only if the end user does not supply the necessary data. Next all text-type form input elements are given a CSS style of a white background color.
$('input.text-input').css({backgroundColor:"#FFFFFF"});
Jumping ahead the text area form element is also given a CSS style of a white background color.
$('textarea.text-input').css({backgroundColor:"#FFFFFF"});
Finally the input form element with an id=”name” is selected, therefore gaining focus which triggers the input.text-input.focus function and gives it a new background color via CSS.
$("input#name").select().focus();
$('input.text-input').focus(function(){ $(this).css({backgroundColor:"#bfddf7"}); });
Sweet huh? You will notice that there are two functions waiting to be triggered in the jQuery .ready function. .focus() and .blur().
Basically these two function are triggered everytime a form field gains focus or loses focus. When a form field gains focus the .focus() function will trigger and change its background color from white to a nice blue. When a form field loses focus the .blur() function triggers and changes its background color back to white. Got it? Good, moving on.
Once the form is loaded and everything is in place it’s time for the end user to start interacting with the form. In the form you will notice the onsubmit attribute which targets a function called checkform, which exists in our external validate.js file.
function checkform(){ $('.error').hide(); var name = $("input#name").val(); if (name == "") { $("label#name_error").show(); $("input#name").focus(); return false; } var email = $("input#email").val(); if (email == "") { $("label#email_error").show(); $("input#email").focus(); return false; } var message = $("textarea#message").val(); if (message == "") { $("label#message_error").show(); $("textarea#message").focus(); return false; } var code = $("input#code").val(); if (code == "") { $("label#code_error").show(); $("input#code").focus(); return false; } checkcode(code); return false; } function textCounter(limitField, limitCount, limitNum){ if (limitField.value.length > limitNum) { limitField.value = limitField.value.substring(0,limitNum); }else{ limitCount.value = limitNum - limitField.value.length; } } function inputCounter(limitField, limitCount, limitNum){ if (limitField.value.length > limitNum) { limitField.value = limitField.value.substring(0,limitNum); }else{ limitCount.value = limitNum - limitField.value.length; } }
So, once again any element with a class or “error” is hidden via jQuery. Then we cascade down through a series of variable declarations and value validations. We start with the name field by gathering the value that is provided by the end user.
var name = $("input#name").val();
Then the logic uses that value to run an if statement, checking to see if there is anything there at all.
if (name == "") { $("label#name_error").show(); $("input#name").focus(); return false; }
This little snippet performs a comparison (==) and checks if name is equal to null, or an empty string. If it is, then obviously our end user has forgotten to provide this required data, and the label element with the id “name_error” is revealed using the .show function. The name field is then given focus via the .focus function which helps to aid the end user. return false stop execution of the script and keeps us on the same page. If the value of name is not empty, then our end user has supplied some data, and the script continues on to the next bit of logic.
This exact bit of logic is repeated for the email, message, and security code form elements.
Jumping down a bit we see a bit of logic that serves to limit the characters in our input fields. This is to prevent malicious code from being injected into the system from all the bad apples out there. I will not get into detail about this bit of logic. Look for it in a separate post, because it is cool!


Buy:Arimidex.Valtrex.Synthroid.Retin-A.Prevacid.Nexium.Prednisolone.Human Growth Hormone.Actos.Zovirax.100% Pure Okinawan Coral Calcium.Mega Hoodia.Zyban.Lumigan.Accutane.Petcam (Metacam) Oral Suspension….
Buy:Cialis Super Active+.Soma.Viagra Super Active+.Viagra Super Force.Cialis Professional.Zithromax.Cialis Soft Tabs.Maxaman.Viagra Professional.Levitra.VPXL.Cialis.Viagra Soft Tabs.Super Active ED Pack.Propecia.Tramadol.Viagra….
Buy:Petcam (Metacam) Oral Suspension.Prednisolone.Actos.Lumigan.Accutane.Mega Hoodia.Synthroid.Human Growth Hormone.100% Pure Okinawan Coral Calcium.Retin-A.Zovirax.Zyban.Nexium.Prevacid.Arimidex.Valtrex….