如何在提交表单的同时触发另一个表单?
我有一个这样的表单
<form action="formprocess1.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="checkbox" name="newsletter" value="1">
<input type="submit" name="submit">
</form>
如果在第一个表单上选中了复选框,我想触发另一个表单
<form action="newsletter.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
谁能给我一些 php 代码或 javascript 代码来自动触发它? 谢谢
I have a form like this
<form action="formprocess1.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="checkbox" name="newsletter" value="1">
<input type="submit" name="submit">
</form>
If the checkbox is checked on first form i want to trigger another form
<form action="newsletter.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
Can anyone give me some php code or javascript code to auto trigger it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Jquery 库.. 像这样的东西.. 我刚刚为您编写了所有代码,但是您需要添加样式和一些 AJAX 内容才能提交,并且它可以工作:
Use the Jquery library.. something like this.. I just made all the code for you, but you need to add styling and some AJAX stuff for submission, and it works:
为此,您可以使用 AJAX(异步 Javascript 和 XML)。如果您使用 jQuery 库,以下是一些如何设置的示例:
http://api.jquery.com/jQuery.post/
You could use AJAX (Asynchronous Javascript And XML) for this. If you use the jQuery library, here are some examples of how you could set this up:
http://api.jquery.com/jQuery.post/
的目标
仅使用一种表单并使用jquery来实现使用不同脚本来处理提交事件javascript
use only one form and use jquery to accomplish the goal of having different scripts to handle the submit event
javascript
您不能同时触发两个表单,因为您一次只能向 PHP 页面发送一个请求,除非您使用 AJAX。
这里的解决方案是隐藏第二个表单,除非附加了复选框,并在选中复选框时将其显示为第一个表单的一部分。
您想让我演示一下如何做到这一点吗?
更新
在更改复选框状态时显示/隐藏新闻通讯表单:
HTML:
Javascript (jQuery):
我尚未测试上述内容,但它应该可以工作,如果有任何错误,请告诉我,我会修复它们。我已经清楚地列出了注释来解释每个步骤的作用。
在 PHP 脚本中,您必须测试该复选框是否已选中,然后进行相应的验证。否则您将始终收到时事通讯信息。仅仅因为它们被隐藏并不意味着它们没有被提交。
我建议将两个单独的 PHP 脚本合并到一个文件中,这将使您的工作变得更加简单。
替代方案是 AJAX,它稍微复杂一些,但随着当今越来越多的人使用它,它是一个很好的实践。如果您想查看这方面的示例,请告诉我。
You cannot trigger two forms at once as you can only send one request to a PHP page at once, unless you are using AJAX.
The solution here would be to hide the second form unless the checkbox is attached, and show it as part of the first form when the checkbox is checked.
Would you like me to demonstrate how you can do this?
Update
Showing/hiding the newsletter form on change of the checkbox state:
HTML:
Javascript (jQuery):
I have not tested the above but it should work, if there are any bugs, let me know and I will fix them. I have clearly set out comments to explain what each step is doing.
In your PHP script you must then test to see if the checkbox is checked, and then validate accordingly. Otherwise you will always receive the newsletter information. Just because they are hidden does not mean they are not submitted.
I would advise combining both of your separate PHP scripts into one file, this will make it much simpler for you.
The alternative to this is AJAX, which is a little more complex but it is good practice as more and more people are using it nowadays. If you would like to see an example of this then let me know.