用一个按钮提交GET表单和另一个POST表单

时间:2014-10-12 09:57:13

标签: javascript php forms request

我正在尝试使用一个提交按钮提交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请求根本没有加载。 我怎样才能解决这个问题? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

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

执行此操作的方式不可能是提交表单时控件转到服务器处理http请求的原因。因此,一次只能提交一份表格。您不能一次提交两个表格。尝试更改表单提交的顺序,其他表单将开始提交..

答案 1 :(得分: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