2014-10-12 46 views
0

我想提交一個POST和一個GET表單提交一個按鈕。我嘗試使用以下的PHP代碼:提交GET表單和另一個POST表單有一個按鈕

echo "<html>\n<head>\n<title>Params</title>"; 
echo "<script type='text/javascript'>"; 
echo "function submitForms(){ 
    document.getElementById('form1').submit(); 
    document.getElementById('form2').submit(); 
}"; 
echo "</script>"; 
echo "</head>"; 
echo "<body>"; 
echo "<form action='' method='get' id='form1'>"; 
echo "<label>First Name </label>"; 
echo "<input type='text' name='first'><br/>"; 
echo "<label>Last Name </label>"; 
echo "<input type='text' name='last'><br/>"; 
echo "<label>Age </label>"; 
echo "<input type='text' name='age'><br/>"; 
echo "<label>Telephone </label>"; 
echo "<input type='text' name='phone'><br />"; 
echo "</form>"; 
echo "<form action='' method='post' id='form2'>"; 
echo "<label>Username </label>"; 
echo "<input type='text' name='username'>"; 
echo "<label>Password </label>"; 
echo "<input type='password' name='password'>"; 
echo "</form>"; 
echo "<input type='submit' value='Send' onclick='submitForms();'>"; 
echo "</body></html>"; 

我得到的只是POST PARAMS,而GET請求根本不加載。 我該如何解決這個問題? 在此先感謝。

+0

您可以刪除回顯和引號,然後使其作爲簡單的html運行。 – Vagabond 2014-10-12 10:00:21

+0

只要調用'.submit()',頁面就會重新加載,腳本的其餘部分將停止。進行多次提交的唯一方法是使用AJAX。 – Barmar 2014-10-12 10:02:58

+0

和你需要兩者的原因?爲什麼不使用一個? – Ghost 2014-10-12 10:03:00

回答

0

你應該只有一種形式在和獲取所有輸入數據後的所有字段提交後,你可以做任何你想做的事..

你這樣做的方式是不可能的原因是,當窗體提交的控件去服務器處理http請求。因此一次只能提交一份表格。您不能一次提交兩個表單。嘗試更改表單提交順序,其他表單將開始提交。

0

您應該使用AJAX(jQuery)。像這樣的應該做的伎倆:

//Onclick for any button you wish to submit the forms 
$('#form2 input[name="submit"]').click(function(e) { 
    e.preventDefault(); 
    var formOne = $("#form1"), 
     formTwo = $("#form2"); 

    //Post first form 
    $.post(formOne.attr('action') , formOne.serialize(), function() { 
     alert('Form one posted!'); 
    }); 

    //Post second form 
    $.post(formTwo.attr('action') , formTwo.serialize(), function() { 
     alert('Form two posted!'); 
    }); 
}); 

尚未測試,但這應該工作。有關$.post方法的更多信息,請參閱here

相關問題