[英]Submit same form with two action?
我有一个表单,可以通过POST提交并将数据发送到2页。
我已经尝试使用javascript代码。 一种表单提交有效,而另一种提交无效
<form id="add">
<input type="text" name="test">
<input type="submit" onclick="return Submit();">
</form>
javascript
function SubmitForm()
{
document.forms['add'].action='filecreate.php';
document.forms['add'].submit();
document.forms['add'].action='filecreate.fr.php';
document.forms['add'].submit();
return true;
}
第一个提交无效,但是第二个提交有效。
由于您似乎将完全相同的数据发送给两个不同的处理程序,因此您可以掷硬币-说您只提交一种表单,然后在filecreate.php
中对它们进行filecreate.php
。
在发送表单时,您无法在同一HTTP请求中发送两个单独的表单-因此,您既可以通过异步方法来完成它们,也可以在提交一个表单后在后台处理它们。
由于您尚未显示任何PHP代码,因此我做出了一些假设并编写了一些伪代码,但这足以使您入门。
因此,首先,为表单设置一个静态的动作属性。
<form id="add" action="filecreate.php">
<input type="text" name="test">
<input type="submit">
</form>
如果要通过POST发送,则还需要指定方法,
<form id="add" action="filecreate.php" method="POST">
然后,在PHP中,如果将两个文件都包含在内,则可以同时执行这两个文件。 filecreate.php
,在您的filecreate.php
,包括了filecreate.fr.php
。 完成后,该文件的内容也将被执行。
<?php
// Once you require the file, it will be executed in place
require "filecreate.fr.php";
// .. handle the rest of your normal execution here.
就是说,如果您使用不同的数据多次执行非常相似的操作,则可能要为其创建函数-按照DRY原理(“不要重复自己”),您可以创建一个函数处理结构和处理,然后通过该函数分别发送数据。
尝试这个 :
<form id="add">
<input type="text" name="test">
<input type="button" onclick="return SubmitForm();">
</form>
function SubmitForm()
{
if(document.forms['add'].onsubmit())
{
document.forms['add'].action='filecreate.php';
document.forms['add'].submit();
document.forms['add'].action='filecreate.fr.php';
document.forms['add'].submit();
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.