How to take values of two fomulários separated by only one Ubmit?

Asked

Viewed 189 times

-1

I have two forms on the same page (well separated by html tags) and I need to take the data of both for my PHP script. Since I am brand new in PHP and I have no idea, I kicked an attempt by triggering PHP in both Forms and putting the input=Submit only in 1 of them, like:

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

Yeah, it didn’t make much sense and I saw that it doesn’t happen. I did some research but I didn’t find anything useful either. Can I do something to make this possible? (This example shows how the structure of my code is)

  • Obviously the names of the elements of the Forms are different, it is not?

  • The first form has Submit button?

  • @dvd the first one does not have Submit, pq just need to use his data on the same page, hence use Javascript

1 answer

1


You can use Javascript to inject in the second form the elements of the first. For this I have dynamically created a div occult (display: none;) in the second form and inserted the contents of the first one. When submitting, all will be sent together.

For that, I put ids in both forms to facilitate selection in Javascript and added onsubmit in 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 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;
}
  • I didn’t even test, but looking at logic I saw that it makes sense! thank you very much, saved me :p

  • If you want to give a quick test at this link: http:/dvdteste.hospedagemdesites.ws/teste.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.