2011-12-03 104 views
1

如果驗證通過,我使用以下代碼發送POST請求。但事實並非如此。有人能告訴我一個方法使其工作。通過javascript中的函數發送POST

<script type="text/javascript"> 

    function go() 
    { 
    var username = document.form.name.value; 
    var pwd = document.form.pwd.value; 
    if (username == '') { 
     var oldHTML = document.getElementById('myAnchor').innerHTML="Please fill out username."; 
    var newHTML = "<span style='color:red'>" + oldHTML + "</span>"; 
    document.getElementById('myAnchor').innerHTML = newHTML; 
    } 
    if (pwd == '') { 
     var oldHTML = document.getElementById('myAnchor2').innerHTML="Please fill out password."; 
    var newHTML = "<span style='color:red'>" + oldHTML + "</span>"; 
    document.getElementById('myAnchor2').innerHTML = newHTML; 
    } 
     else { 
     document.form.method="POST"; 
     document.form.action = "data.php"; 
    } 
    </script> 

回答

1

一切看起來正確的提交表單,您只需要實際提交表單。將以下行添加到您的else區塊中,如下所示:

else { 
    document.form.method="POST"; 
    document.form.action = "data.php"; 
    //submit the form -- new line is below 
    document.form.submit(); 
    } 
0
下面

的片斷如何通過JavaScript

<form name="myform" action="handle-data.php"> 
Search: <input type='text' name='query' /> 
<a href="javascript: submitform()">Search</a> 
</form> 
<script type="text/javascript"> 
function submitform() 
{ 
    document.myform.submit(); 
} 
</script>