1 $(document).ready(function(){
2 $('#send_message').click(function(e){
4 //stop the form from being submitted
7 /* declare the variables, var error is the variable that we use on the end
8 to determine if there was an error or not */
10 var name = $('#name').val();
11 var email = $('#email').val();
12 var subject = $('#subject').val();
13 var message = $('#message').val();
15 /* in the next section we do the checking by using VARIABLE.length
16 where VARIABLE is the variable we are checking (like name, email),
17 length is a javascript function to get the number of characters.
18 And as you can see if the num of characters is 0 we set the error
19 variable to true and show the name_error div with the fadeIn effect.
20 if it's not 0 then we fadeOut the div( that's if the div is shown and
21 the error is fixed it fadesOut.
23 The only difference from these checks is the email checking, we have
24 email.indexOf('@') which checks if there is @ in the email input field.
25 This javascript function will return -1 if no occurence have been found.*/
28 $('#name_error').fadeIn(500);
30 $('#name_error').fadeOut(500);
32 if(email.length == 0 || email.indexOf('@') == '-1'){
34 $('#email_error').fadeIn(500);
36 $('#email_error').fadeOut(500);
38 if(subject.length == 0){
40 $('#subject_error').fadeIn(500);
42 $('#subject_error').fadeOut(500);
44 if(message.length == 0){
46 $('#message_error').fadeIn(500);
48 $('#message_error').fadeOut(500);
51 //now when the validation is done we check if the error variable is false (no errors)
53 //disable the submit button to avoid spamming
54 //and change the button text to Sending...
55 $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
57 /* using the jquery's post(ajax) function and a lifesaver
58 function serialize() which gets all the data from the form
59 we submit it to send_email.php */
60 $.post("send_email.php", $("#contact_form").serialize(),function(result){
61 //and after the ajax request ends we check the text returned
63 //if the mail is sent remove the submit paragraph
64 $('#button').remove();
65 //and show the mail success div with fadeIn
66 $('#mail_success').fadeIn(500);
68 //show the mail failed div
69 $('#mail_fail').fadeIn(500);
70 //reenable the submit button by removing attribute disabled and change the text back to Send The Message
71 $('#send_message').removeAttr('disabled').attr('value', 'Submit');