简体   繁体   English

用两个动作提交相同的表格?

[英]Submit same form with two action?

I have a form to submit and send data to 2 pages via POST. 我有一个表单,可以通过POST提交并将数据发送到2页。

I have tried the code with javascript. 我已经尝试使用javascript代码。 One form submit is working but other submit is not working 一种表单提交有效,而另一种提交无效

<form id="add">
    <input type="text" name="test">
    <input type="submit" onclick="return Submit();">
</form>

javascript 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;
}

The first submission is not working but 2nd submission is working. 第一个提交无效,但是第二个提交有效。

Since you appear to send the exact same data to two different handlers, you can flip the coin - and say that you just submit one form, and process them both in filecreate.php . 由于您似乎将完全相同的数据发送给两个不同的处理程序,因此您可以掷硬币-说您只提交一种表单,然后在filecreate.php中对它们进行filecreate.php

As you are sending a form, you cannot send two separate forms in the same HTTP request - so you can either do them both through asynchronous methods, or process them both backend after the submission of one form. 在发送表单时,您无法在同一HTTP请求中发送两个单独的表单-因此,您既可以通过异步方法来完成它们,也可以在提交一个表单后在后台处理它们。

Since you haven't shown any PHP code, I'm making some assumptions and writing some pseudo-code, but it should be enough to get you started. 由于您尚未显示任何PHP代码,因此我做出了一些假设并编写了一些伪代码,但这足以使您入门。

So first off, set a static action-property to your form. 因此,首先,为表单设置一个静态的动作属性。

<form id="add" action="filecreate.php">
   <input type="text" name="test">
   <input type="submit">
</form>

If you are sending it over POST, then you need to specify the method as well, 如果要通过POST发送,则还需要指定方法,

<form id="add" action="filecreate.php" method="POST">

Then, in PHP, you can get both files executed if you include it to the other. 然后,在PHP中,如果将两个文件都包含在内,则可以同时执行这两个文件。 Meaning, in your filecreate.php , you include the filecreate.fr.php . filecreate.php ,在您的filecreate.php ,包括了filecreate.fr.php Once you do that, the contents of that file will be executed as well. 完成后,该文件的内容也将被执行。

<?php 
// Once you require the file, it will be executed in place
require "filecreate.fr.php";

// .. handle the rest of your normal execution here.

That said, if you are doing the very similar thing multiple times, just with different data, you may want to create functions for it instead - going with the DRY principle ("Don't Repeat Yourself"), you can probably create a function that handles the structure and processing, then send the data separately through that function. 就是说,如果您使用不同的数据多次执行非常相似的操作,则可能要为其创建函数-按照DRY原理(“不要重复自己”),您可以创建一个函数处理结构和处理,然后通过该函数分别发送数据。

Try this : 尝试这个 :

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM