I am a beginner at PHP and was working on a contact form.
After selecting the submit button, the response message is not shown. The page goes to the PHP file and the email is never sent. Also, none of the error messages appear.
I am using validate.js and Jquery form.js. Here is a reference link:http://www.tutwow.com/htmlcss/create-a-simple-and-secure-contact-form-with-jquery-and-php/
The style of the contact form is similar to the one featured on the Clicky Media page (Click get a quote on the site.) HTML:
JavaScript
x
30
30
1
<div class='contentform'>
2
3
<form id="contact_form" action="contactform.php" method="post" novalidate="nonvalidate">
4
5
<div class="form">
6
<select>
7
<option value="Select Topic">--Select Topic--</option>
8
<option value=""></option>
9
<option value=""></option>
10
<option value=''></option>
11
12
</select>
13
14
<input kl_virtual_keyboard_secure_input="on" id="overlay_name" name="overlay_name" value="First Name" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" type="text"></input>
15
<input kl_virtual_keyboard_secure_input="on" id="overlay_name2" name="overlay_name2" value="Last Name" class="right" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" type="text"></input>
16
<input kl_virtual_keyboard_secure_input="on" id="overlay_telephone" name="overlay_telephone" value="Phone" class="left" onfocus="if (this.value == 'Phone') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Phone';}" type="text"></input>
17
<input kl_virtual_keyboard_secure_input="on" id="overlay_email" name="overlay_email" value="Email" class="right" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" type="email"></input>
18
19
20
21
<textarea style="overflow: hidden; word-wrap: break-word; resize: none; height: 27px;" rows="1" id="overlay_message" name="overlay_message" onfocus="if (this.value == 'Message') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Message';}"> Message </textarea>
22
</div>
23
<div class='formfooter'>
24
25
<input id="send" type='submit' name='sumbit' value ='send message'></input>
26
</div>
27
</form>
28
<div id="response"></div>
29
</div>
30
Javascript:
JavaScript
1
76
76
1
$('#contactform').validate({
2
// Specify what the errors should look like
3
// when they are dynamically added to the form
4
errorElement: "input",
5
errorPlacement: function(error, element) {
6
error.insertBefore( element.parent().parent() );
7
error.wrap("<input class='ErrorField'></input>");
8
9
},
10
11
// Add requirements to each of the fields
12
rules: {
13
overlay_name: {
14
required: true,
15
minlength: 3
16
},
17
overaly_name2:{
18
required:true,
19
minlength:3
20
},
21
overlay_email: {
22
required: true,
23
email: true
24
},
25
overlay_telephone:{
26
required:true,
27
minlength:10,
28
expression: "if (VAL != 'Phone') return true; else return false;",
29
30
},
31
overlay_message: {
32
required: true,
33
minlength: 10
34
}
35
},
36
37
// Specify what error messages to display
38
// when the user does something horrid
39
messages: {
40
overlaye_name: {
41
required: "Please enter your name.",
42
minlength: jQuery.format("At least {0} characters required.")
43
},
44
overlaye_name2: {
45
required: "Please enter your last name.",
46
minlength: jQuery.format("At least {0} characters required.")
47
},
48
overlay_email: {
49
required: "Please enter your email.",
50
email: "Please enter a valid email."
51
},
52
overlay_telephone:{
53
required:"Please enter telephone number.",
54
phone:"Please enter valid number."
55
},
56
57
overlay_message: {
58
required: "Please enter a message.",
59
minlength: jQuery.format("At least {0} characters required.")
60
}
61
},
62
63
// Use Ajax to send everything to processForm.php
64
submitHandler: function(form) {
65
$("#send").attr("value", "Sending...");
66
$(form).ajaxSubmit({
67
target: "#response",
68
success: function(responseText, statusText, xhr, $form) {
69
70
$("#response").html(responseText).hide().slideDown("fast");
71
}
72
});
73
return false;
74
}
75
});
76
PHP:
JavaScript
1
123
123
1
// Clean up the input values
2
foreach($_POST as $key => $value) {
3
if(ini_get('magic_quotes_gpc'))
4
$_POST[$key] = stripslashes($_POST[$key]);
5
6
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
7
}
8
9
// Assign the input values to variables for easy reference
10
$name=$_POST["overlay_name"];
11
$name2=$_POST["overlay_name2"];
12
$telephone=$_POST["overlay_telephone"];
13
$email=$_POST["overlay_email"];
14
$message=$_POST["overlay_message"];
15
16
// Test input values for errors
17
$errors = array();
18
if(strlen($name) < 2) {
19
if(!$name) {
20
$errors[] = "You must enter a name.";
21
} else {
22
$errors[] = "Name must be at least 2 characters.";
23
}
24
}
25
if(!$email) {
26
$errors[] = "You must enter an email.";
27
} else if(!validEmail($email)) {
28
$errors[] = "You must enter a valid email.";
29
}
30
if(strlen($message) < 10) {
31
if(!$message) {
32
$errors[] = "You must enter a message.";
33
} else {
34
$errors[] = "Message must be at least 10 characters.";
35
}
36
37
38
if($errors) {
39
// Output errors and die with a failure message
40
$errortext = "";
41
foreach($errors as $error) {
42
$errortext .= "<li>".$error."</li>";
43
}
44
die("<span class='failure'>The following errors occured:<ul>". $errortext ."</ul></span>");
45
}
46
47
48
$to = "email address here";
49
$subject = "Contact Form: $name";
50
$message = "$message";
51
$headers = "From: $email";
52
53
mail($to, $subject, $message, $headers);
54
55
// Die with a success message
56
die("<span class='success'>Success! Your message has been sent.</span>");
57
58
// A function that checks to see if
59
// an email is valid
60
function validEmail($email)
61
{
62
$isValid = true;
63
$atIndex = strrpos($email, "@");
64
if (is_bool($atIndex) && !$atIndex)
65
{
66
$isValid = false;
67
}
68
else
69
{
70
$domain = substr($email, $atIndex+1);
71
$local = substr($email, 0, $atIndex);
72
$localLen = strlen($local);
73
$domainLen = strlen($domain);
74
if ($localLen < 1 || $localLen > 64)
75
{
76
// local part length exceeded
77
$isValid = false;
78
}
79
else if ($domainLen < 1 || $domainLen > 255)
80
{
81
// domain part length exceeded
82
$isValid = false;
83
}
84
else if ($local[0] == '.' || $local[$localLen-1] == '.')
85
{
86
// local part starts or ends with '.'
87
$isValid = false;
88
}
89
else if (preg_match('/\.\./', $local))
90
{
91
// local part has two consecutive dots
92
$isValid = false;
93
}
94
else if (!preg_match('/^[A-Za-z0-9\-\.]+$/', $domain))
95
{
96
// character not valid in domain part
97
$isValid = false;
98
}
99
else if (preg_match('/\.\./', $domain))
100
{
101
// domain part has two consecutive dots
102
$isValid = false;
103
}
104
else if(!preg_match('/^(\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/',
105
str_replace("\\","",$local)))
106
{
107
// character not valid in local part unless
108
// local part is quoted
109
if (!preg_match('/^"(\\"|[^"])+"$/',
110
str_replace("\\","",$local)))
111
{
112
$isValid = false;
113
}
114
}
115
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
116
{
117
// domain not found in DNS
118
$isValid = false;
119
}
120
}
121
return $isValid;
122
}
123
Advertisement
Answer
You are not at all connecting your submitHandler
with the <form>
‘s submit
event. Do this:
JavaScript
1
4
1
$(function () {
2
$("#contactForm").submit(submitHandler);
3
});
4