user2710234
user2710234

Reputation: 3225

submit a form inside another form

I have a form with POST method and an action of another page.

Within the form i have another form that I need to make submit with a different action but its submitting with the main form action.

this is my second form:

<script>
    function formSubmit()
    {
        document.getElementById("invoices_form").submit();
    }
</script>

<form action="resend_multiple_invoices.php" name="invoices_form" method="post">
    <input type="button" onclick="formSubmit()" value="Send Invoices" />
</form>

how can i get it to submit the second form and not the main one?

Upvotes: 3

Views: 36034

Answers (3)

Jack Kral
Jack Kral

Reputation: 149

You can use the 'form'-attribute in your input-fields and then mix all your inputs. By submitting they refer to the correct form.

<form action="" method="post" id="form1"></form>
<form action="" method="post" id="form2"></form>

<input name="firstname" form="form1">
<input name="firstname" form="form2">

<button type="submit" name="submit" form="form1">Save form 1</button>
<button type="submit" name="submit" form="form2">Save form 2</button>

See also https://www.w3schools.com/tags/att_input_form.asp

Upvotes: 11

entitycs
entitycs

Reputation: 347

JQuery.ajax and html for validating an "inner form" through ajax, then submitting the entire form. I use ajax in both cases to show the purpose of a controller.php file and a submission id. You could also have an inner form which consists of several segregated sections by using classes instead of ids as Jquery selectors.

<form>
    <input />
    <textarea />
    <select /> <!-- etc. -->
    <section id="verify">
        <input />
        <textarea />
        <select /> <!-- etc -->
        <button type="button">submit</button>
        <!-- eg. sub-submission verifies data in section -->
    </section>
    <select />
    <input />
    <input type="submit" value="submit" />
</form>
<script>
$(document).ready(function() {

   $("#verify button").on ('click', verify);
   $('form').submit (formSend);

   function verify (){
       // get input data within section only (ie. within inner form)
       var postData = $('#verify').filter(':input' ).serializeArray();
       postData.push ({name:"submitId", value:'verify'});
       var request = $.ajax ({
          type: "POST",
          url: "controller.php",
          data: postData,
          error: function (xhr, status, message){
             alert (status);
          }
       });
   }
   function formSend (){
       // get input data within entire form
       var postData = $(this).serializeArray();
       postData.push ({name:"submitId", value:'send'});
       var request = $.ajax ({
          type: "POST",
          url: "controller.php",
          data: postData,
          error: function (xhr, status, message){
             alert (status);
          }
       });
   }
});
</script>

Upvotes: 0

Mr Jones
Mr Jones

Reputation: 1198

You cannot (universally) submit a nested form separately from its parent form. Nested forms are invalid HTML as outlined in the W3C prohibitions.

To solve your problem, I suggest you use two separate forms as follows:

<script>
    function invoicesFormSubmit()
    {
       document.getElementById("invoices_form").submit();
    }

    function otherFormSubmit()
    {
       document.getElementById("other_form").submit();
    }
</script>

<form action="resend_multiple_invoices.php" name="invoices_form" method="post">

    //
    // Input fields go here
    //

    <input type="button" onclick="invoicesFormSubmit()" value="Send Invoices" />
</form>

<form action="other_method.php" name="other_form" method="post">

    //
    // Input fields go here
    //

    <input type="button" onclick="otherFormSubmit()" value="Other Method" />
</form>

Upvotes: 10

Related Questions