[英]Page redirect with successful Ajax request
我有一个使用Ajax进行客户端验证的表单。 表格的结尾如下:
$.ajax({
url: 'mail3.php',
type: 'POST',
data: 'contactName=' + name + '&contactEmail=' + email + '&spam=' + spam,
success: function(result) {
//console.log(result);
$('#results,#errors').remove();
$('#contactWrapper').append('<p id="results">' + result + '</p>');
$('#loading').fadeOut(500, function() {
$(this).remove();
});
}
});
编辑:这是我的mail3.php文件处理错误:
$errors=null;
if ( ($name == "Name") ) {
$errors = $nameError; // no name entered
}
if ( ($email == "E-mail address") ) {
$errors .= $emailError; // no email address entered
}
if ( !(preg_match($match,$email)) ) {
$errors .= $invalidEmailError; // checks validity of email
}
if ( $spam != "10" ) {
$errors .= $spamError; // spam error
}
if ( !($errors) ) {
mail ($to, $subject, $message, $headers);
//header ("Location: thankyou.html");
echo "Your message was successfully sent!";
//instead of echoing this message, I want a page redirect to thankyou.html
} else {
echo "<p id='errors'>";
echo $errors;
echo "</p>";
}
我想知道如果ajax请求成功且没有错误,是否可以将用户重定向到Thank You页面。 这可能吗?
谢谢! 阿米特
当然。 只需在成功函数的最后添加一些内容,例如:
if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"
当没有错误时,服务器返回响应no_errors
。
只需做一些错误检查,如果一切都通过,那么设置window.location
将用户重定向到另一个页面。
$.ajax({
url: 'mail3.php',
type: 'POST',
data: 'contactName=' + name + '&contactEmail=' + email + '&spam=' + spam,
success: function(result) {
//console.log(result);
$('#results,#errors').remove();
$('#contactWrapper').append('<p id="results">' + result + '</p>');
$('#loading').fadeOut(500, function() {
$(this).remove();
});
if ( /*no errors*/ ) {
window.location='thank-you.html'
}
}
});
您可以在success
处理程序中重定向,如下所示:
window.location.href = "thankyou.php";
或者,因为您正在显示结果,请等待几秒钟,例如,这将等待2秒:
setTimeout(function() {
window.location.href = "thankyou.php";
}, 2000);
在你的mail3.php文件中,你应该在try {} catch {}中捕获错误
try {
/*code here for email*/
} catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
}
然后在你的success
电话中你不必担心你的错误,因为它永远不会成功。
你可以使用: window.location.href = "thankyou.php";
在你的成功功能中,如尼克说。
$.ajax({
url: 'mail3.php',
type: 'POST',
data: 'contactName=' + name + '&contactEmail=' + email + '&spam=' + spam,
success: function(result) {
//console.log(result);
$('#results,#errors').remove();
$('#contactWrapper').append('<p id="results">' + result + '</p>');
$('#loading').fadeOut(500, function() {
$(this).remove();
});
if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"
}
});
我认为你可以这样做:
window.location = "your_url";
我想你可以用两种方式攻击它;
1)将window.location = 'http://www.yourdomain.com
插入成功函数。
2)使用另一个ajax调用将此注入到页面上的元素中,您可以在http://api.jquery.com/jQuery.get/上的jQuery文档中找到更多信息。
我在不同的帖子上发布了确切的情况。 重新发布。
对不起,这不是上面提到的问题的答案。
但是带来了一个有趣的话题---何时使用AJAX以及何时不使用AJAX。 在这种情况下,最好不要使用AJAX。
我们来看一个简单的登录和密码示例。 如果登录名和/或密码不匹配,那么使用AJAX报告一条说“登录不正确”的简单消息会很好。 但是如果登录名和密码是正确的,为什么我必须回调一个AJAX函数来重定向到用户页面?
在这种情况下,我认为使用简单的表单SUBMIT会很不错。 如果登录失败,重定向到Relogin.php,它看起来像Login.php一样,在网址中有一个GET消息,如Relogin.php?error = InvalidLogin ......就像那样......
只需2美分。 :)
另一种选择是:
window.location.replace("your_url")
// Create lag time before redirecting
setTimeout(function() {
window.location.href = "thankyou.php";
}, 2000);
$errors=null;
if ( ($name == "Name") ) {
$errors = $nameError; // no name entered
}
if ( ($email == "E-mail address") ) {
$errors .= $emailError; // no email address entered
}
if ( !(preg_match($match,$email)) ) {
$errors .= $invalidEmailError; // checks validity of email
}
if ( $spam != "10" ) {
$errors .= $spamError; // spam error
}
if ( !($errors) ) {
mail ($to, $subject, $message, $headers);
echo "Your message was successfully sent!";
//instead of echoing this message, I want a page redirect to thankyou.html
// redirect
setTimeout();
} else {
echo "<p id='errors'>";
echo $errors;
echo "</p>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.