2 Forms On Same Page Php
Solution 1:
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters. If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<formname="form_a"action="<?phpecho$_SERVER['PHP_SELF']; ?>"method="POST"><inputtype="hidden"name="form"value="A"><buttontype="submit">Form A</button></form><formname="form_b"action="<?phpecho$_SERVER['PHP_SELF']; ?>"method="POST"><inputtype="hidden"name="form"value="B"><buttontype="submit">Form B</button></form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case"A":
echo"submitted A";
break;
case"B":
echo"submitted B";
break;
default:
echo"What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you. Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url
is the action params of the form
Solution 2:
Your form should be like this. First form
<form method="post" >
...
<inputtype="hidden" name="form1submission" value="yes" >
<inputtype="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<inputtype="hidden" name="form2submission" value="yes" >
<inputtype="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
Solution 3:
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<formmethod="post"><inputtype="hidden"name="orderform" /><!-- rest of form goes here --></form><formmethod="post"><inputtype="hidden"name="klant_gegevens" /><!-- rest of form goes here --></form>
Leaving the action
-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"]
can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values
Post a Comment for "2 Forms On Same Page Php"