2012-03-08 156 views
0

我不確定我想要做什麼可能沒有PHP或一些其他腳本語言,所以我會很感激的一些建議。Javascript表單提交

我們有一個只能提供靜態HTML的web服務器,所以我們有點受限。我們希望將使用JavaScript的表單上的電子郵件註冊數據從本網站上的另一個域的dataHandler.php腳本發送到將其保存到數據庫的dataHandler.php腳本。當客戶點擊提交時,我不希望頁面導航到dataHandler.php,雖然在我希望它以某種方式刷新的其他域(我不知道如何),並感謝加入我們的電子郵件列表。下面是我想到的代碼示例。

任何關於如何實現它的建議都會感激不盡,或者任何意見說停止浪費你的時間也是有幫助的。

<script type="text/javascript"> 
<!-- 
function validate() { 
if (document.emailForm.email.value.length==0) { 
alert("You forgot to enter your email address"); 
return false; 
}  
if (document.emailForm.email.value.length>0) { 
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; 
var address = document.emailForm.email.value; 
if(reg.test(address) == false) { 
    alert('Invalid email address'); 
    return false; 
    } 
} 
document.emailForm.submit() 
return true; 
} 
//--> 
</script> 

<form id="emailForm" name="emailForm" action="http://www.otherdomain.com/dataHandler.php" method="post"> 
    <input size="30" id="email" name="email" maxlength="200" /> 
    <input onclick="validate();" type="button" value="Submit" /> 
</form> 

回答

0

你應該看看jQuery表單插件(http://jquery.malsup.com/form/)。它完全符合你的需求,並且可以用一行代碼實現。

+0

令人驚歎的感謝! – 2012-03-08 23:05:23