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;
});
});
ここで何が間違っているのですか?専門家のアドバイスは本当に役に立ちます。