如何使用单个提交按钮在一页中提交 2 个表单
我创建了一个包含两个表单的 php 页面,但我希望这两个表单都只有一个提交按钮。表单具有 id
s firstform
& 第二种形式
。我尝试过其他脚本,但它们实际上不起作用。
下面是我的代码:
<script language="javascript">
submitForms = function(){
document.getElementById("firstform").submit();
document.getElementById("secondform").submit();
}
</script>
<input type="image" src="images/order-button.png" name="button" value="Submit" onclick="submitForms()"/>
I've created a php page with two forms but I would like to have only one submit button for both forms. the forms have the id
s firstform
& secondform
. I have tried other scripts but they don't really work.
Here is my code below:
<script language="javascript">
submitForms = function(){
document.getElementById("firstform").submit();
document.getElementById("secondform").submit();
}
</script>
<input type="image" src="images/order-button.png" name="button" value="Submit" onclick="submitForms()"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您有几个问题
input type=image 是一个提交按钮,因此您尝试从不存在的表单提交某些内容,可能与您所在的页面相同
当您提交 form1 时,它会替换当前页面,如果您也设法提交 form2,则很可能会干扰提交form1
这是您可以尝试的方法(纯javascript):
或者一次使用AJAX表单(假设加载了 jQuery)
DEMO HERE
更新:如果你想要两个如果要将表单的内容提交给一个操作,只需添加序列号即可:
PS:演示中的 PHP 只是回显您发布的内容。服务器上不需要执行任何特殊操作。
You have SEVERAL issues
input type=image IS a submit button so you are trying to submit something from a non-existing form, likely the same page you are on
when you submit form1, it replaces the current page, if you manage to submit form2 as well, it is VERY likely to interfere with the submission of form1
Here is what you can TRY (plain javascript):
Alternatively AJAX the forms one at a time (assumes jQuery loaded)
DEMO HERE
UPDATE: If you want two form's content to be submitted to one action, just add the serialises:
PS: The PHP in the demo is just echoing back what you post. There is no special action needed on the server.
这是要演示的 jsfiddle
http://jsfiddle.net/TqhPr/18/
HTML
JavaScript
Here's the jsfiddle to demonstrate.
http://jsfiddle.net/TqhPr/18/
HTML
JavaScript
我用它来解决类似的问题。我想创建一个页面来查询多个站点并并排查看它们的结果:
WordLookup.html(main/home/frame 入口点):
然后是frame.html(javascript方法):
Sorry if this is a little wordy but it's a working example (in chrome 56ish).
I used this for a similar problem. I wanted to create a single page to query multiple sites and review their results side-by-side:
WordLookup.html (main/home/frame entry point):
and then this for frame.html (javascript method):
Sorry if this is a little wordy but it's a working example (in chrome 56ish).
此代码使用单个提交按钮成功发布了 2 个表单数据。
This code successfully posted 2 forms data with single submit button.