2012-09-04 26 views
1

我想使這個簡單的iContact表單不重定向到成功頁面或錯誤頁面。我只希望我的訪問者提交表單,而不必將其從頁面重定向或重新加載頁面。強制iContact註冊表單不能重定向到不同的頁面

我一直在環顧四周,但我無法解決這個問題。這裏是我的IContact接口形式的代碼(我代替我的帳戶信息):

<style> 
.link, 
.link a, 
.signupframe 
{ 
    color: #000000; 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 13px; 
    } 
    .link, 
    .link a { 
     text-decoration: none; 
     } 
    .signupframe { 
     border: 1px solid #000000; 
     background: #ffffff; 
     } 
.signupframe .required { 
    font-size: 10px; 
    } 
</style> 
<form method="post" action="https://app.icontact.com/icp/signup.php" name="icpsignup" id="icpsignupAAAA" accept-charset="UTF-8" onsubmit="return verifyRequiredAAAA();" > 
<input type="hidden" name="redirect" value="http://www.icontact.com/www/signup/thanks.html"> 
<input type="hidden" name="errorredirect" value="http://www.icontact.com/www/signup/error.html"> 

<div id="SignUp"> 
<table width="260" class="signupframe" border="0" cellspacing="0" cellpadding="5"> 
    <tr> 
     <td valign="top" align="right"> 
     <span class="required">*</span> Email 
     </td> 
     <td align="left"> 
     <input type="text" name="fields_email"> 
     </td> 
    </tr> 
    <input type="hidden" name="listid" value="42379"> 
    <input type="hidden" name="specialid:42379" value="O6IC"> 

    <input type="hidden" name="clientid" value="954091"> 
    <input type="hidden" name="formid" value="4616"> 
    <input type="hidden" name="reallistid" value="1"> 
    <input type="hidden" name="doubleopt" value="0"> 
    <tr> 
     <td> </td> 
     <td><span class="required">*</span> = Required Field</td> 
    </tr> 
    <tr> 
     <td> </td> 
     <td><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
    </table> 
</div> 
</form> 
<script type="text/javascript"> 

var icpForm4616 = document.getElementById('icpsignupAAAA'); 

if (document.location.protocol === "https:") 

    icpForm4616.action = "https://app.icontact.com/icp/signup.php"; 
function verifyRequired4616() { 
    if (icpForm4616["fields_email"].value == "") { 
    icpForm4616["fields_email"].focus(); 
    alert("The Email field is required."); 
    return false; 
    } 


return true; 
} 
</script> 

回答

0

後而不是在提交與AJAX的形式。使用與上面相同的onSubmit調用,只需通過AJAX發佈它,然後返回false而不是發佈到action屬性值。在下面的例子中,我使用jQuery庫的Ajax功能,所以如果你沒有它,你必須下載並安裝它...

刪除您的JavaScript在底部,然後添加此代替。確保你已經下載並安裝了你的FTP中的jQuery文件。同時正確地添加HTML結構與HTML,頭部和身體標記:http://www.w3.org/TR/html401/struct/global.html#h-7.1

<script type="text/javascript" src="/js/jquery.1.7.2.min.js"></script> 
<script type="text/javascript"> 
function verifyRequiredAAAA() 
{ 

    var eReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; // regex to check valid email 
    var email = $('input[name="fields_email"]').val(); 
    if (email == "") { 
     $('input[name="fields_email"]').focus(); 
     alert("The Email field is required."); 
     return false; 
    } else if (!eReg.test(email)) { 
     $('input[name="fields_email"]').focus(); 
     alert("The Email address is incorrect."); 
     return false; 
    } else { 
     $.post("https://app.icontact.com/icp/signup.php", 
      $("#icpsignupAAAA").serialize(), function(data){ 
        $('#successcontainer').html(data); 
      }); 
      return false; 
    } 
} 

</script> 

然後在你的HTML放在一個容器中一個潛在的成功消息:

<div id="successcontainer"></div> 

(該消息在這裏來自你的PHP腳本(PHP的應該返回的東西......))

更改您的FORM標籤:

<form id="icpsignup4616" onsubmit="return verifyRequired4616()" accept-charset="UTF-8" name="icpsignup" action="" method="post"> 

你也可以例如取出完整形式,如果你在成功的函數(successcontainer行後),這些行的網址,您希望它重定向到成功添加

$("#icpsignupAAAA").hide(); 
+0

如果您將表單onSubmit返回爲false,表單永遠不會將您帶走,而是讓jQuery發佈數據並從PHP腳本獲取答覆。 – jtheman

+0

感謝您的回答,請您向我解釋一下如何做到這一點,以及如何嵌入您在主代碼中提供的代碼,因爲我是一名初學者,不知道AJAX,謝謝。 – Michael

+0

你能否請我向我解釋如何退回表單onSubmit爲false,謝謝。 – Michael

0

變化值或錯誤。

<input type="hidden" name="redirect" value="http://www.icontact.com/www/signup/thanks.html"> 
<input type="hidden" name="errorredirect" value="http://www.icontact.com/www/signup/error.html"> 
+0

我不希望它從我的網站以任何方式重定向,我只是希望它在沒有重定向的情況下提交。 – Michael

0

由於跨域(CORS)限制,似乎不可能做你想要的。如果你想嘗試使用jQuery的解決方案,有人建議,你會得到的東西沿着這些路線:

XMLHttpRequest cannot load https://app.icontact.com/icp/signup.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

UPDATE

蘇氨酸的要求似乎達到IContact接口服務器。但是,除了控制檯中的錯誤之外,AJAX請求被認爲是失敗的。

相關問題