我正在使用 ajax 联系表单,但它似乎无法正常工作。一旦我尝试提交表单,我就会收到“请填写所有字段”的消息,即使我已经填写了所有字段。
这是我当前的表单代码:
<form method="get" action="mail.php">
<input name="fname" type="text" value="Enter your First Name here..." id="fname" onfocus="if(this.value == 'Enter your First Name here...') this.value = ''"/>
<input name="lname" type="text" value="Enter your Last Name here..." id="lname" onfocus="if(this.value == 'Enter your Last Name here...') this.value = ''"/>
<input name="cname" type="text" value="Enter your Company Name here..." id="cname" onfocus="if(this.value == 'Enter your Company Name here...') this.value = ''"/>
<input name="web" type="text" value="Enter your Website here..." id="web" onfocus="if(this.value == 'Enter your Website here...') this.value = ''"/>
<input name="email" type="text" value="Enter your E-mail here..." id="email" onfocus="if(this.value == 'Enter your E-mail here...') this.value = ''"/>
<input name="phone" type="text" value="Enter your Phone Number here..." id="phone" onfocus="if(this.value == 'Enter your Phone Number here...') this.value = ''"/>
<textarea name="message" id="message" onfocus="if(this.value == 'Enter your message here...') this.value = ''" rows="" cols="">Enter your message here...</textarea>
<input name="submit" type="submit" value="Submit" id="submit"/>
<div id="response" style="float:right">
<p></p>
</div>
</form>
我的 mail.php 的内容:
<?php
if ($_GET['fname'] == "" || $_GET['lname'] == "" || $_GET['cname'] == "" || $_GET['web'] == "" || $_GET['phone'] == "" || $_GET['email'] == "" || $_GET['message'] == "") {
echo 'Please fill in all fields!';
exit;
}
if (strlen($_GET['fname']) < 3) {
echo 'Your name should be atleast 3 characters long!';
exit;
}
if (strlen($_GET['lname']) < 3) {
echo 'Your name should be atleast 3 characters long!';
exit;
}
if (strlen($_GET['cname']) < 3) {
echo 'Your name should be atleast 3 characters long!';
exit;
}
if (strlen($_GET['web']) < 3) {
echo 'Your name should be atleast 3 characters long!';
exit;
}
if (strlen($_GET['phone']) < 3) {
echo 'Your name should be atleast 3 characters long!';
exit;
}
if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_GET['email'])) {
echo 'Please enter a valid email address!';
exit;
}
if (strlen($_GET['message']) < 10) {
echo 'Your message should be atleast 10 characters long!';
exit;
}
$to = 'my@email.com';
$name = stripslashes($_GET['name']);
$email = stripslashes($_GET['email']);
$subject = "Contact request";
$msg = "From:".$name."\r\n";
$msg .= "e-Mail:".$email."\r\n";
$msg .= "Subject:".$subject."\r\n\n";
$msg .= "---Message--- \r\n".nl2br(stripslashes($_GET['message']))."\r\n\n";
$mail = mail($to, $subject, $msg, "From:".$email);
if($mail) {
echo 'We will be in touch shortly!';
} else {
echo 'Error: Message could not be sent!';
}
?>
最后但并非最不重要的 ajax.js
$(function() {
$("input#submit").click(
function() {
$('#response p').empty().append("<img src='img/ajax-loader.gif' />");
var fname = $("#fname").val();
var lname = $("#lname").val();
var cname = $("#cname").val();
var web = $("#web").val();
var email = $("#email").val();
var phone = $("#phone").val();
var message = $("#message").val();
var dataString = 'fname='+ fname + 'lname='+ lname + 'cname='+ cname + 'web='+ web + '&email=' + email + 'phone='+ phone + '&message=' + message;
$.ajax({
url: "mail.php",
data: dataString,
type: "GET",
success: function(responseText) {
$('#response p').empty().append(responseText);
}
});
return false;
});
});
知道这里有什么问题吗?一些专家的建议会很有帮助。