一键提交GET表单和另一个POST表单


Submit GET form and another POST form with one button

我正试图用一个提交按钮提交POST和GET表单。我尝试使用以下php代码:

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参数,而get请求根本没有加载。我该怎么解决这个问题?提前谢谢。

你应该只接受一个表单中的所有字段,在提交后获得所有输入数据后,你可以做任何你想做的事情。

这样做是不可能的,原因是当表单提交时,控件会转到服务器来处理http请求。因此一次只能提交一份表格。你不能同时提交两份表格。。尝试更改表单提交的顺序,其他表单将开始提交。。

您应该使用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方法的更多信息,请参阅此处。