我是一個PHP黑客。洞察力非常感謝,因爲它會幫助我找出我出錯的地方。Ajax聯繫表格上的PHP驗證失敗
基本上我改編了this contact form爲我自己的目的。 Javascript驗證效果很好!這似乎像PHP驗證也正常工作。直到我開始從我自己的表格獲得空白提交。我正在嘗試避免在此表單中添加驗證碼。空白提交很煩人,如果有人能指出我是否在我的改編中犯了錯誤,我會很喜歡它。感謝您的時間。
HTML表單
<?php include('/ajax/verify.php');?>
<form action="/ajax/" method="post" id="sendEmail">
<h4>Contact Us</h4>
<p class="alert">* All fields are required</p>
<ol class="forms">
<li><label for="username">Your Name</label><input type="text" name="username" id="username" value="" /></li>
<li><label for="emailFrom">Your Email</label><input type="text" name="emailFrom" id="emailFrom" value="" /></li>
<li><label for="phonenumber">Phone Number</label><input type="text" name="phonenumber" id="phonenumber" value="" /></li>
<li><label for="message">Message</label><textarea name="message" id="message"></textarea></li>
<li class="buttons"><button type="submit" id="submit">Send Email »</button><input type="hidden" name="submitted" id="submitted" value="true" /></li>
</ol>
</form>
JavaScript驗證
//Ajax Form
$(document).ready(function(){
$("#submit").click(function(){
$(".error").hide();
var hasError = false;
var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/;
var phoneReg = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
//from email
var emailFromVal = $("#emailFrom").val();
if(emailFromVal == '') {
$("#emailFrom").after('<span class="error">You forgot to enter the email address to send from.</span>');
hasError = true;
} else if(!emailReg.test(emailFromVal)) {
$("#emailFrom").after('<span class="error">Enter a valid email address to send from.</span>');
hasError = true;
}
//name
var usernameVal = $("#username").val();
if(usernameVal == '') {
$("#username").after('<span class="error">You forgot to enter your name.</span>');
hasError = true;
}
//phone
var phonenumberVal = $("#phonenumber").val();
if(phonenumberVal == '') {
$("#phonenumber").after('<span class="error">You forgot to enter your phone number.</span>');
hasError = true;
} else if(!phoneReg.test(phonenumberVal)) {
$("#phonenumber").after('<span class="error">Enter a valid phone number.</span>');
hasError = true;
}
//message
var messageVal = $("#message").val();
if(messageVal == '') {
$("#message").after('<span class="error">You forgot to enter the message.</span>');
hasError = true;
}
if(hasError == false) {
$(this).hide();
$("#sendEmail li.buttons").append('<img src="/ajax/img/ajax-loader.gif" alt="Loading" id="loading" />');
$.post("/ajax/sendEmail.php",
{ emailFrom: emailFromVal, username: usernameVal, phonenumber: phonenumberVal, message: messageVal },
function(data){
$("#sendEmail").slideUp("normal", function() {
$("#sendEmail").before('<h4 class="success">Thank You</h4><p class="success">One of our highly trained staff will contact with you shortly.</p>');
});
}
);
}
return false;
});
});
驗證腳本(PHP)
if(isset($_POST['submitted'])) {
if($_POST['emailFrom'] == '') {
$emailFromError = 'You forgot to enter the email address to send from.';
} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", $_POST['emailFrom'])) {
$emailFromError = 'Enter a valid email address to send from.';
}
if($_POST['phonenumber'] == '') {
$emailFromError = 'You forgot to enter the email address to send from.';
} else if (!eregi("/^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/$", $_POST['phonenumber'])) {
$emailFromError = 'Enter a valid email address to send from.';
}
if($_POST['message'] == '') {
$messageError = 'You forgot to enter the message.';
}
if($_POST['username'] == '') {
$messageError = 'You forgot your name.';
}
if(!isset($emailFromError) && !isset($messageError)) {
include('sendEmail.php');
include('thanks.php');
}
}
Mailscript
$mailTo = '[email protected]';
$mailFrom = $_POST['emailFrom'];
$username = $_POST['username'];
$phonenumber = $_POST['phonenumber'];
$subject = "New website inquiry from $username";
$message = $_POST['message'];
$message = wordwrap($message, 70);
$messagebody = "From: $username Phone Number: $phonenumber $message";
mail($mailTo, $subject, $messagebody, "From: ".$mailFrom);
何時/你在哪裏調用你的php驗證腳本? – jeroen
你的Apache日誌說什麼? (除了建議不要使用'eregi'。 – cwallenpoole
@ jeroen嗯......那肯定會解釋問題的一部分。現在更新。 –