How do I get values from two fomulars separated by just one submit?

0

I have two forms on the same page (well separated by html tags) and I need to get the data of the two for my PHP script. As I'm brand new to PHP and I have no idea, I kicked an attempt by triggering PHP in both forms and putting the input = submit only on 1 of them, type:

<form action="meu.php" metod="POST">
conteudo do form1
</form>
<-tags HTML, como div,a,center etc->
<form action="meu.php" metod="POST">
conteúdo do form2
<input type="submit">
</form>

That's right, it did not make much sense and I saw that it does not roll. I gave one searched but also found nothing useful. Can I do something to make it possible? (this example serves to show how the structure of my code is)

    
asked by anonymous 15.03.2018 / 17:17

1 answer

1

You can use JavaScript to inject in the second form elements of the first. For this I dynamically created a div hidden ( display: none; ) in the second form and inserted the contents of the first one. By submitting it, everything will be sent together.

For this, I put id s on both forms to make it easier to select in JavaScript and added onsubmit on the second to call the function.

HTML would look like this:

<form id="form1" action="meu.php" method="POST">
   conteúdo do form1
</form>
<form id="form2" action="meu.php" method="POST" onsubmit="return formularios()">
   conteúdo do form2
   <input type="submit">
</form>

And the JavaScript:

function formularios(){

   var div = document.createElement("div");
   div.setAttribute("id","divoculta");
   div.style.display = "none";
   document.querySelector("#form2").appendChild(div);
   var form1html = document.querySelector("#form1").innerHTML;
   document.querySelector("#divoculta").innerHTML = form1html;

   return true;
}
    
15.03.2018 / 17:45