Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:

Copy<form action="" method="post">
    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <button id="clickAll">Submit All</button>
</form>

And in php:

Copyforeach ($_POST['name'] as $key => $value) {
    $_POST['name'][$key]; // make something with it
    $_POST['example'][$key];  // it will get the same index $key
}
Answer from Renato Probst on Stack Overflow
Top answer
1 of 4
5

Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:

Copy<form action="" method="post">
    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <input type="text" name="name[]">
    <input type="text" name="example[]">

    <button id="clickAll">Submit All</button>
</form>

And in php:

Copyforeach ($_POST['name'] as $key => $value) {
    $_POST['name'][$key]; // make something with it
    $_POST['example'][$key];  // it will get the same index $key
}
2 of 4
0

Here is a working jsFiddle: http://jsfiddle.net/SqF6Z/3/

Basically, add a class to each form and trigger() a submit on that class. Like so:

HTML (example only):

Copy<form action="http://www.google.com" method="get" class="myForms" id="1stform">
    <input type="text" value="1st Form" name="q1" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
    <input type="text" value="2nd Form" name="q2" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
    <input type="text" value="3rd Form" name="q3" />
</form>
<input type="button" id="clickMe" value="Submit ALL" />

jQuery:

Copy$('.myForms').submit(function () {
    console.log("");
    return true;
})

$("#clickMe").click(function () {
    $(".myForms").trigger('submit'); // should show 3 alerts (one for each form submission)
});
🌐
SitePoint
sitepoint.com › php
Submit multiple form with one button - PHP - SitePoint Forums | Web Development & Design Community
June 28, 2022 - As we can see, i have used post method for form submission, it insert user input data into database with: · Your if/elseif/if conditions are in PHP so if you display the page in an a browser and use ‘View Page Source’ you will not see them in the HTML · Your pseudo code and written ...
Discussions

One submit button to processed two forms - PHP - SitePoint Forums | Web Development & Design Community
Hello Everyone My question is quite simple but more difficult to convert into the PHP and HTML code. The idea is to upload a file to the server and email data from the form using one submit button. The HTML Body looks… More on sitepoint.com
🌐 sitepoint.com
0
February 17, 2021
Submit Multiple Forms With One Button - formit - MODX Community
Hello, I have 2 form on the same page: First form is embed form from external website (vTiger webform) with action="https://***/crm/modules/Webforms/capture.php" method="post" Second form is FormIt-based form with preHook, which generate “form name”, “user_id” and “form_id” (value ... More on community.modx.com
🌐 community.modx.com
1
December 7, 2021
html - Submitting multiple forms to same action page using one button in php - Stack Overflow
So I am trying to send data from multiple forms on the same page to the same action page using only one submit button. So far I have tried using the form ID for all my forms and it is still unsuccessful. The first form is the only one that goes thru and I think that is because the action on its FORM tag, is executed before the rest of the forms can also be executed. Below I have put some sample code illustrating what I am try to do: More on stackoverflow.com
🌐 stackoverflow.com
PHP Multiple Forms Best Practice
Not necessarily. You can have all the forms functionality in one document and define what function/action should be run depending on what submit button has been clicked f.ex. Let's f.ex. say you have one of your submit buttons named ok (or whatever, this is just for the simplicity of the example); You can then look for that button-push in your php document; if(isset($_POST['ok'])) { // Some action } This could be used for everyone of your buttons, say an ok2 perhaps; if(isset($_POST['ok2'])){...} and so forth. I'd say it's a little about preferences how you do it. If the functionality is little, then I'd probably keep it in the same document. Are there gonna be a lot of functionality, which would mean a document with lots of code, then it might be a good idea to break it down into smaller, separate documents. More on reddit.com
🌐 r/PHP
23
14
July 22, 2017
🌐
TechRepublic
techrepublic.com › home › handling multiple submits in a single form with php
Handling multiple submits in a single form with PHP - TechRepublic
June 8, 2007 - Since a form can only have a single action, the same PHP script had to handle all four tasks, by identifying which button had been clicked and executing the appropriate code branch. Thus, a need arose for a single form carrying multiple submit buttons, and a form-processing script with the intelligence to distinguish between each one...
🌐
SitePoint
sitepoint.com › php
One submit button to processed two forms - PHP - SitePoint Forums | Web Development & Design Community
February 17, 2021 - The idea is to upload a file to the server and email data from the form using one submit button. The HTML Body looks like this: and PHP like this: if(isset($_FILES['file']) OR ($_POST['email'])) { code for upload a file......
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 255723 › two-forms-one-submit-button-hw-does-it-work
php - Two forms one submit button, HW DOES IT ... | DaniWeb
— chrishea 182 Jump to Post · It is possible to use no submit buttons and instead just have a regular button <input type="button" and use a javascript function onclick to submit the two forms.
🌐
MODX Community
community.modx.com › t › submit-multiple-forms-with-one-button › 4714
Submit Multiple Forms With One Button - formit - MODX Community
December 7, 2021 - Hello, I have 2 form on the same page: First form is embed form from external website (vTiger webform) with action="https://***/crm/modules/Webforms/capture.php" method="post" Second form is FormIt-based form with preHook, which generate “form ...
🌐
CodePen
codepen.io › uzielweb › pen › mrwYvr
Submit Two Forms at same time with one button only
<form id="form1" action="publishform.php"> <input name="Name" /> </form> <form id="form2"> <input name="Name" /> </form> <button id="submit">Submit</button> ! CSS Options · Format CSS · View Compiled CSS · Analyze CSS · Maximize CSS Editor ...
Find elsewhere
Top answer
1 of 2
1

Technically you are submitting all the forms, but they all have the same action and ID. Check out this question: multiple-forms-and-one-processing-page

Firstly the HTML ID attribute must be unique. Once each form has it's own ID you can then properly parse the data in the $_GET variable from within your example.php file's action.

2 of 2
0

You can do this only using javascript and send the 3 request in the background.

For example. Mark your forms with specified class, for example multiple_form.

Then change your code to something like:

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<button class="multiple_form_sender" name="sub1">Submit</button>

Then add javascript to your code

var forms = document.getElementsByClassName("multiple_form");

var sender = function(e) {
  const form = e.target;

  // Post data using Fetch
  fetch(form.action, {
    method: form.method,
    body: new FormData(form)
  })

  // Prevent the default form submit
  e.preventDefault();
};

for (var i = 0; i < forms.length; i++) {
    forms[i].addEventListener('submit', sender);
}

document.getElementById("multiple_form_sender").addEventListener('click', function(){
    for (var i = 0; i < forms.length; i++) {
        forms[i].submit();
    }
});

But if you don't want to send forms using javascript. You should consider one form with array parameters.

🌐
Reddit
reddit.com › r/php › php multiple forms best practice
r/PHP on Reddit: PHP Multiple Forms Best Practice
July 22, 2017 -

I'm new to PHP, so this question might be self-explanatory given more knowledge of the language.

When handling multiple forms from the same html file, what is the best practice in terms of PHP file structure? Should each form be pointing to a different PHP file, should a single PHP file handle every form somehow?

Thanks for the advice in advance.

Top answer
1 of 4
11

You have SEVERAL issues

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

  2. 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):

<script language="javascript">
function submitForms() {
  document.getElementById("firstform").submit();
  document.getElementById("secondform").submit();
 }
</script>

<form id="firstform" target="iframe1">
</form><iframe name="iframe1" style="display:none"></iframe>
<form id="secondform" target="iframe2">
</form><iframe name="iframe1" style="display:none"></iframe>
<button typ"button" onclick="submitForms()"><img src="images/order-button.png" "/></button>

Alternatively AJAX the forms one at a time (assumes jQuery loaded)

DEMO HERE

$(document).ready(function() {
  $("#subbut").click(function(e) {
    e.preventDefault(); // or make the button type=button
    $.post($("#firstform").attr("action"), $("#firstform").serialize(), function() {
      $.post($("#secondform").attr("action"), $("#secondform").serialize(),
        function() {
          alert('Both forms submitted');
        });
    });
  });
});

UPDATE: If you want two form's content to be submitted to one action, just add the serialises:

$(document).ready(function() {
  $("#subbut").click(function(e) {
    e.preventDefault(); // or make the button type=button
    $.post($("#firstform").attr("action"), $("#firstform").serialize() + $("#secondform").serialize(), function() {
      alert('Both forms submitted');
    });
  });
});

PS: The PHP in the demo is just echoing back what you post. There is no special action needed on the server.

2 of 4
3
  • Set the "target" attribute on the first form to "_blank"
  • Set the "action" attribute on the first form to "#close" (replace "close" with whatever you want.
  • Have a script on the page that checks if the document.location.hash is "close" if it is window.close()

Here's the jsfiddle to demonstrate.

http://jsfiddle.net/TqhPr/18/

HTML

<form id="f1" name="f1" target="_blank" method="POST" action="#close">
    <input type="hidden" value="1" />
    <input type="submit" id="s1" value="11" />
</form>
<hr/>
<form id="f2" name="f2" method="POST" action="#second_form">
    <input type="hidden" value="2" />
    <input type="submit" id="s2" value="22" />
</form>
<hr/>
<input type="button" id="both" value="Submit Both Forms" />

JavaScript

$(document).ready(function() {
    $("#both").click(function() {
        document.getElementById("f1").submit();
        document.getElementById("f2").submit();
    });
    if(document.location.hash == "#close") {
        alert("closing the window caused by posting the first form");
        window.close();
    }
    if(document.location.hash) {
        alert(document.location.hash);
    }
});
Top answer
1 of 3
1

Since is your form and you are using SQL to get and the objective is to update the data with one submit?

Why not just use one form?. I belive you can do all you need with just one and multiple input.

2 of 3
1

Your branch if(isset($_GET["dg_no"]) && !empty(trim($_GET["dg_no"]))){ depends on get parameters, however, your <form method="post"> of <input type="hidden" name="dg_no" value="<?php echo $dg_no; ?>"/> uses post parameters.

Therefore the branch will never be executed unless the page is requested by another GET-request like a link or another form.

If the parameters can occur in both methods, POST and GET as well, you might want to check the $_REQUEST array instead. Be aware that the parameters listed in $_REQUEST can vary depending on the .ini settings request_order and variables_order.

According to your comment 'Because each form is for one table row.' in another answer, this might be an XY problem.

Consider to take the common way not generating multiple forms but array parameters similar as you did in

<input type="hidden" name="answer1[]" id = "$answer1" value="<?php echo $answer; ?>"/>

Here you rely on keys getting generated automatically. As well you can specify an individual key:

<input type="text" name="some_parameter[<?php echo $answer; ?>]">

Further more

There is an HTML line in the loop having a static id on an element:

<input type="hidden" name="consideration_no[]" id="consideration_no" value="<?php echo $consideration_no; ?>"/>

This will not break PHP, however, it is against the HTML specs saying an id has to be unique per document.

🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
[SOLVED] Multiple forms with only one submit button? - PHP Coding Help - PHP Freaks
February 2, 2009 - Is it possible to only use one submit button to update all the values of a query in a form? What I have is the results of a query associated with input fields. There will be up to 10 results per page. Each has it's own individual entry in the database. Can I go through somehow and update all the ...
Top answer
1 of 1
4

One way you could solve this issue of seperating your data on the PHP side would be to add a name array to your inputs. For example:

<form name="form1" method="post">
    <input type="text" name="test[1]" />
    <input type="text" name="test[2]" />
    <input type="submit" value="Submit" />
</form>

Then after you submit to a PHP page you can access them by:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    echo $_POST['test'][1] . $_POST['test2'][2];
}

If you absolutely have to have separate form tags for some unknown reason, then you can also do this using AJAX. Add a class to your submit buttons and bind a click event to this using vanilla JavaScript and or jQuery, cancel the normal form action, and post to a standalone php page. AJAX will return w/e you write back. Here is an Example of the JavaScript:

jQuery('.submit-button').click(function() {

    //Find all input fields in the form and store them
    var fields = jQuery(this).parent().find(':input').serializeArray(),
        url = '/somepage/somepath/',
        returnJSON,
        ajaxRequestHandler;

    //Stop the form from submitting
    event.preventDefault(); 

    //Start AJAX request
    ajaxRequestHandler = jQuery.ajax({
        type: "POST",
        url: url,
        data: fields
    });

    //Error Hanlder
    ajaxRequestHandler.fail(function (returnData) {
        console.log('failure');
    });

    //Success Handler
    ajaxRequestHandler.done(function (returnData) {
       //Display a message to the html page.
    });


});

Here is the HTML:

<form name="form1" method="post">
    <input type="text" name="test1" class="submit-button" />

</form>

<form name="form1" method="post">
    <input type="text" name="test2" />
    <input type="submit" value="Submit" class="submit-button" />
</form>

You then need to setup a standalone PHP page or a new method in your controller if your using MVC. You also need to make sure this PHP page is secure.

Top answer
1 of 5
7

when I click on submit for any particular form, it submits all the forms.

this is not true.
Once your forms have proper formatting, your browser will submit only current one.
(and PHP has nothing to do here)

however, whole page will be reloaded, if you mean that. That is okay - when you submit a form, a page is intended to reload. If you need another behavior, you have to explain your wishes.

Also note that none of your text fields being sent to the server as they have no names.

I guess the question I should be asking is, how do I pass a particular form to php instead of writing multiple php scripts to handle each form!!!

well, it seems you want to ask how to distinguish these forms.

add a hidden field into each

<input type="hidden" name="step" value="1" />

and then in PHP

if ($_POST['step'] == 1) {
  //first form
}
if ($_POST['step'] == 2) {
  //second
}
2 of 5
3

This submits one form of many to php. Copy, paste, test, and study.

<?php
     if (isset($_POST['submitForm'])) { 

    print_r($_POST);

     }

?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>

Using a,b,c,d for the first form, e,f,g,h for the second form and i,j,k,l for the third form and submitting the second form yields the following output:

Array
(
    [A] => e
    [B] => f
    [C] => g
    [D] => h
    [submitForm] => Submit Form
)
🌐
PHPBuilder
board.phpbuilder.com › d › 10351103-submit-two-forms-with-one-button
submit two forms with one button - PHPBuilder Forums
February 14, 2008 - hi, how can i submit two forms with one button, one to send the order to me and the other to paypal? I need it to not contain javascript. I have this one so...