簡體   English   中英

提交Ajax表單后如何將用戶重定向到另一個頁面?

[英]How to redirect user to another page after Ajax form submission?

成功完成表單后,我無法將用戶重定向到“謝謝”頁面。 發生的情況是,在提交表單后,它會轉到空白頁( https://cunet.sparkroom.com/Sparkroom/postLead )...我需要將其重定向到“謝謝”頁面,同時將表單詳細信息提交給“表單操作”中的網址。

HTML代碼:

<form action="https://cunet.sparkroom.com/Sparkroom/postLead/" method="post" name="theForm" 
id="theForm" onSubmit="return MM_validateForm();" >
...
</form>

Ajax代碼:

<script src="http://malsup.github.com/jquery.form.js"></script> 
<script> 
    $('#theForm').ajaxForm(function() { 
    window.location.href = "I want the user to be redirected to this page";
});
</script> 

JavaScript的:

function MM_validateForm() {
if ( !jQuery('#theForm #FirstName').val() ) {
alert('Please input your first name.');
jQuery('#theForm #FirstName').focus();
return false;
}
if ( !jQuery('#theForm #LastName').val() ) {
alert('Please input your last name.');
jQuery('#theForm #LastName').focus();
return false;
}
if ( !jQuery('#theForm #daytimephone').val() ) {
alert('Please input your phone number.');
jQuery('#theForm #daytimephone').focus();
return false;
}
if ( !jQuery('#theForm #Email').val() ) {
alert('Please input your email.');
jQuery('#theForm #Email').focus();
return false;
}
if ( !jQuery('#theForm #BID').val() ) {
alert('Please select your preferred campus.');
jQuery('#theForm #BID').focus();
return false;
}
if ( !jQuery('#theForm #programs').val() ) {
alert('Please select your preferred program.');
jQuery('#theForm #programs').focus();
return false;
}
if ( !jQuery('#theForm #How_Heard').val() ) {
alert('Please select how you heard about us.');
jQuery('#theForm #How_Heard').focus();
return false;
}
return true;
}
// ]]></script>

有人知道我在做什么錯嗎? 我需要將表單提交到URL的表單,然后將用戶重定向到“謝謝”頁面之后,這根本沒有發生。

嘗試

window.location.href = "http://www.msdn.com";

嘗試:

$('#theForm').ajaxForm(function() { 
 success : function(){
    window.location.href = "Url to redirect here in the success option";
 }
});

成功發布表單后,有兩種方法可以重定向用戶。

1)您可以在ajax提交中放置一個on Success事件,您可以在其中簡單地將用戶重定向到thankyou頁面。

2)在PHP中使用Ex完成表單后處理后,使用服務器端功能:

header("Location: http://thankyoupage.html");

您可以使用jquery處理提交帖子,因此您必須取消表單中的操作

    <form  name="theForm" id="theForm" >

只需給“提交”按鈕提供課程或ID

    <submit class='submitTheForm'....>

代碼將是這樣的..您只需要包含所有具有正確名稱的表單變量。

然后將jQuery post請求添加到MM_validateForm()的底部

    if ( !jQuery('#theForm #How_Heard').val() ) {
    alert('Please select how you heard about us.');
    jQuery('#theForm #How_Heard').focus();
    return false;
    }

    $.post("https://cunet.sparkroom.com/Sparkroom/postLead/",
{
FirstName:FirstName,
LastName:LastName
    ...
    ...
},
function(){
    window.location.href = 'thank you page'
    }
});

最后,當單擊提交按鈕時,調用MM_validateForm()函數

    $('.submitTheForm').click( function(){
    MM_validateForm();
    });

來自http://api.jquery.com/jQuery.post/的jquery帖子來自其他個人經驗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM