在成功提交表单后,如何让我的页面重定向?
目前,在提交表单时出现错误,错误会显示在表单上方。
提交表单时没有错误,成功页面显示在表单上方的错误消息容器中(而不是重定向)。
我的Ajax:
function checkform() {
var form = $('form')[0];
var data = new FormData(form);
$.ajax({
url: 'upload.php',
data: data ,
processData: false,
contentType: false,
dataType: 'text',
cache: false,
type: 'POST',
success: function(data){
$("div#error").html(data).slideDown("fast");
//Scroll to top of this div - 70px from the top of your view, change this to whatever number you wish
var destination = $('div#uploadContainer').offset().top - 15;
$("html:not(:animated),body:not(:animated)").animate({
scrollTop: destination
}, 200);
}
});
return false;
}
我的PHP(在验证之后......)
if (isset($_POST['messageSubmit'])) {
header('Location: ' . $message_location);
}
if (isset($_POST['drawingSubmit'])) {
header('Location: ' . $drawing_location);
}
if (isset($_POST['postSubmit'])) {
header('Location: ' . $other_location);
}
答案 0 :(得分:0)
您需要在javascript success
功能中重定向您的网页,这是通过window.location.href = "http://yourNewLocation"
完成的。在您的情况下,我会将onSubmit = "checkForm()"
替换为onSubmit = "return false"
,您可以在输入中添加<input type="submit" name="drawingSubmit" value="Upload now" onClick = "checkForm('drawingSubmit')" />
。现在,在checkForm()
函数中,您可以将所点击的input
的名称作为参数 - “
function checkForm(inputName){
//current code
if(inputName == "drawingSubmit"){
window.location.href = "//wherever you want to redirect the user";
}
}
答案 1 :(得分:0)
在success
回调函数中,您必须设置窗口位置href,如下所示:
window.location.href = 'http://example.com/your-new-path'