简体   繁体   中英

Submit same form with two action?

I have a form to submit and send data to 2 pages via POST.

I have tried the code with 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

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 .

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.

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.

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,

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

Then, in PHP, you can get both files executed if you include it to the other. Meaning, in your filecreate.php , you include the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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