Ami*_*mit 41 javascript ajax jquery redirect
我有一个使用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();
});
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:这是我的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>";
}
Run Code Online (Sandbox Code Playgroud)
我想知道如果ajax请求成功且没有错误,是否可以将用户重定向到Thank You页面.这可能吗?
谢谢!阿米特
Ada*_*dam 37
当然.只需在成功函数的最后添加一些内容,例如:
if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"
Run Code Online (Sandbox Code Playgroud)
no_errors当没有错误时,服务器返回响应的位置.
tj1*_*111 15
只做一些错误检查,如果一切都通过,则设置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'
}
}
});
Run Code Online (Sandbox Code Playgroud)
Nic*_*ver 11
你可以在你的success处理程序中重定向,如下所示:
window.location.href = "thankyou.php";
Run Code Online (Sandbox Code Playgroud)
或者,因为您正在显示结果,请等待几秒钟,例如,这将等待2秒:
setTimeout(function() {
window.location.href = "thankyou.php";
}, 2000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
209868 次 |
| 最近记录: |