2014-09-21 72 views
-1

問題與形式提交 - 信息需要發送到數據庫和需要重定向到感謝頁面問題與形式提交 - 信息數據庫,並重定向到謝謝

下面是代碼和小提琴的環節好。謝謝。

我試圖重定向到 - 謝謝

相反,我重定向到 - 「行動」 - URL

與現有的代碼,也沒有將信息發送到數據庫的問題。它工作正常,只是它不會將我重定向到「謝謝」頁面。

HTML:

<form name="MailingList" method="post" Id="formId" action="URL" onsubmit="return validate_form(this);"> 
<input type="email" name="emailaddress" placeholder="Email Address" maxlength="100" size="28"> 
<br> 
<input type="submit" name="Submit" value="Submit" width="260px"> 
</form> 

var frmvalidator = new Validator("MailingList"); 

frmvalidator.addValidation("Email", "maxlen=50"); 
frmvalidator.addValidation("Email", "req"); 
frmvalidator.addValidation("Email", "email"); 

<script> 
jQuery(function() { 
jQuery('#subscribe').click(function() { 
    jQuery.ajax({ 
     type: 'POST', 
     url: 'URL', 
     data: 'emailaddress=' + jQuery('input[name="emailaddress"]').val(), 
     success: function (data) { 
      if (jQuery(data).find('.subscribe_valid').length) { 
       location.href = 'Thank you page'; 
      } 
     }, 
     error: function() { 
      alert('error handling here'); 
     } 
    }); 
}); 
}); 
</script> 
+0

上面的驗證器在腳本標籤之外,將永遠不會工作。請參閱我的回答您的原始代碼 – mplungjan 2014-09-21 05:38:49

回答

0

先決條件

  1. 形式和腳本需要住在同一個原點爲Ajax目標 - 如果不是,不需要任何進一步的閱讀 - 例如你永遠不能從jsfiddle測試teabeyond。

  2. jQuery有腳本

  3. 驗證必須是jQuery的負載事件處理

然後,你需要 取消表單提交與

....submit(function(e) { 
    e.preventDefault(); 
內之前加載

以下是試圖修復原始問題的代碼。我希望一個序列化的形式,而不是在AJAX的上下文

的jsfiddle不能阿賈克斯到其他網站比的jsfiddle本身這樣的代碼不會在的jsfiddle

FIDDLE

function validate_form(theForm) { 
    if (theForm.emailaddress.value=="") { 
     alert("Please enter email"); 
     return false; 
    } 
    return true; 
} 
$(function() { 
    $('#formId').submit(function (e) { 
    e.preventDefault(); // cancel the form 
    if (validate_form(this)) { 
     // the form's action MUST point to a page on the same origin as the form 
     $.post(this.action, $(this).serialize(), 
     function() { 
      window.location.replace("https://www.othersite.com/Articles.asp?ID=252"); 
     }); 
    } 
    }); 
}); 
工作

而不是$(this).serialize(),你可以有

{"emailaddress2",this.emailaddress2.value} 
+0

不工作!任何其他出路! – crv 2014-09-21 04:29:51

+0

這是主要的錯誤,所以它應該工作,除非有其他js錯誤。查看控制檯 – mplungjan 2014-09-21 04:55:30

+0

- 表單驗證腳本 – crv 2014-09-21 05:50:54

-2

replace是不會做任何事情,因爲你沒有提供第二個參數提供它。替換法是這樣的:

String.replace('texttolookfor', 'replacementtext'); 

String replace

如果您想重定向,那麼你需要給replace方法,你想要去的網址:

window.location.replace("https://www.teabeyond.com/Articles.asp?ID=252", "http://www.teabeyond.com/MailingList_subscribe.asp"); 

你可能想要使用控制檯來驗證您的地址,以及沒有。

而且,每@mplungjan,你需要防止默認提交操作。

+0

Location.replace不是String.replace – mplungjan 2014-09-21 04:55:11

相關問題