3

I have two forms, I need one submit button for two forms. How I can do it, without ajax?

<form action="" id="form" method="post">
    <input type="file" name="file">
    <input type="text" name="text">
</form>

<form action="" id="form2" method="post">
    <input type="file" name="file">
    <input type="text" name="text">
</form>

I know that this topic has been raised more than once, but it's not a duplicate, as I need it without ajax

Mykyta Dudariev
  • 462
  • 5
  • 17
  • 1
    Do you mean ajax or javascript? – LS_ᴅᴇᴠ Aug 11 '17 at 07:38
  • ajax, because in the architecture of the project it's hard to do – Mykyta Dudariev Aug 11 '17 at 07:39
  • 1
    You can't submit 2 forms at the same time without Ajax, it makes no sense. A form submission means serializing all the inputs (to `x-www-form-urlencoded`, `form-data` or leaving it raw) sending them to the `action` URL using POST or GET, and then doing whatever the server says, either navigating away or immediately rendering a response. How would you do 2 of those at the same time? What is your end goal here and why no Ajax? – kaqqao Aug 11 '17 at 07:43
  • The task is allocated 10 hours, but in addition to this task there are several more. In general, I realized that without Ajax, it's unrealistic to do. I still hoped. – Mykyta Dudariev Aug 11 '17 at 07:49

2 Answers2

4

You can use Document.forms which returns a collection.Thereafter forms[0] will return the first form from DOM and so on .Then you can use one button to submit the forms

<button type = "button" onclick="submitForm()">Submit</button>

function submitForm(){
    document.forms[0].submit();
    document.forms[1].submit();
}
brk
  • 48,835
  • 10
  • 56
  • 78
3

You have to declare a button and attach a click event handler for it.

<input type="button" value="Click" onclick="submit()" />

function submit(){
   document.getElementById("form1").submit();
   document.getElementById("form2").submit();
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128