您需要更改響應的行爲,使您可以控制何時顯示成功和錯誤時重定向,檢查這些變化:
sendemail.php:
<?php
/************************
* Variables you can change
*************************/
$mailto = "[email protected]";
$cc = "";
$bcc = "";
$subject = "Email subject";
$vname = "BrightCherry enquiry";
/************************
* do not modify anything below unless you know PHP/HTML/XHTML
*************************/
$email = $_POST['email'];
$resp['error'] = FALSE; // by default there are no errors
function validateEmail($email)
{
if(eregi('^[a-zA-Z0-9._-][email protected][a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
return true;
else
return false;
}
if((strlen($_POST['name']) < 1) || (strlen($email) < 1) || (strlen($_POST['message']) < 1) || validateEmail($email) == FALSE){
$resp['error'] = TRUE;
$emailerror .= 'Error:';
if(strlen($_POST['name']) < 1){
$emailerror .= '<li>Enter name</li>';
}
if(strlen($email) < 1){
$emailerror .= '<li>Enter email</li>';
}
if(validateEmail($email) == FALSE) {
$emailerror .= '<li>Enter valid email</li>';
}
if(strlen($_POST['message']) < 1){
$emailerror .= '<li>Enter message</li>';
}
$err = "<div id='emailerror'><ul>$emailerror</ul></div>";
$resp['error_msg'] = $err;
} else {
$emailerror .= "<span>Your email has been sent successfully!</span>";
// NOW SEND THE ENQUIRY
$timestamp = date("F j, Y, g:ia");
$messageproper ="\n\n" .
"Name: " .
ucwords($_POST['name']) .
"\n" .
"Email: " .
ucwords($email) .
"\n" .
"Comments: " .
$_POST['message'] .
"\n" .
"\n\n" ;
$messageproper = trim(stripslashes($messageproper));
mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['e_mail'].">\nReply-To: \"".ucwords($_POST['first_name'])."\" <".$_POST['e_mail'].">\nX-Mailer: PHP/" . phpversion());
}
echo json_encode($resp);
?>
而且您在您的聯繫人和索引ajaxForm
(我不知道爲什麼你需要它那裏):
$('#submitform').ajaxForm({
dataType: 'json',
success: function(resp) {
if(resp.error)
$('#error').html(resp.error_msg).fadeIn('slow');
else
window.location = 'index.php';
}
});
我做什麼是以下幾點:
- 驗證表單和回聲結果作爲
json
- 如果有錯誤,設置錯誤標誌和錯誤信息
- 否則只要傳遞錯誤標誌設置爲false
- 在你的成功方法,如果你的錯誤標誌被設置爲true,那麼顯示錯誤
- else else redirect!
你想在facebox中加載什麼? – Kyle 2011-01-12 05:59:05
我正在通過驗證加載登錄表單。如果沒有錯誤,我希望facebox關閉並刷新當前頁面。 – 2011-01-12 06:01:37