當表單成功提交時,如何讓頁面重定向?使用PHP重定向進行Ajax調用
當前當表單提交時出現錯誤,錯誤出現在表單上方。
當表單提交時沒有錯誤,成功頁面顯示在窗體上方的錯誤消息容器中(而不是重定向)。
我的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(後驗證等...)你需要重定向頁面在JavaScript success
功能
if (isset($_POST['messageSubmit'])) {
header('Location: ' . $message_location);
}
if (isset($_POST['drawingSubmit'])) {
header('Location: ' . $drawing_location);
}
if (isset($_POST['postSubmit'])) {
header('Location: ' . $other_location);
}
你應該在成功響應中寫入位置 –
如果您試圖在成功回調中獲取標頭,則需要使用* jqXHR * object:'jqXHR.getResponseHeader('Location')' – Hkan
如何指定要在哪個重定向中使用Ajax雖然?在PHP中,重定向取決於點擊哪個提交按鈕。 – DavidPottrell