2014-10-12 66 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

相关问题