Processing Multiple Forms on One Page with PHP
I know that the way most people treat multiple forms on one page is to have each form post to another PHP file where the form is validated, its information is entered into a database or an email is sent off. So you usually have something like this:
<form name="contactform" method="post" action="sendmail.php"> blah blah blah </form> <form name="mailinglist" method="post" action="join.php"> blah blah blah </form>
That work great, but why would you create all those extra files when you can just have the form post to the same file and create multiple functions to process your multiple forms.
The solution is very simple and super efficient.
First, lets create some forms.
<form name="mailinglist" method="post"> <input type="text" name="email" /> <input type="submit" name="submit" value="Join Our Mailing List" /> </form> <form name="contactus" method="post"> <input type="text" name="email" /> <input type="text" name="subjet" /> <textarea name="message"></textarea> <input type="submit" name="submit" value="Send Email" /> </form>
Now lets put some PHP code before the <head> tag to have different processes for each form.
<?php
if (array_key_exists('mailinglist', $_POST)) {
do something here;
}
if (array_key_exists('contact', $_POST)) {
do something here;
}
?>
One more step. You need to add a hidden input tag to each form for it to function properly.
<form name="mailinglist" method="post"> <input type="text" name="email" /> <input type="submit" name="submit" value="Join Our Mailing List" /> <input type="hidden" name="mailinglist" value="1"/> // hidden input tag </form> <form name="contactus" method="post"> <input type="text" name="email" /> <input type="text" name="subjet" /> <textarea name="message"></textarea> <input type="submit" name="submit" value="Send Email" /> <input type="hidden" name="contactus" value="1"/> // hidden input tag </form>
Now all you need to do is create your processes within those two “if” statements and each form will be dealt with accordingly when it it filled and submitted.
The trick is making sure to match the name of the hidden field with the array_key_exists() function in the “if” statement.






Very nicely explained & a useful tip – many thanks!
Dez.
Hey, thanks for the info, but how about showing specifically how to connect the sendmail.php and the join.php to the if statements you made and maybe adding an error notice or die switch if one form is not complete.
I’m going to assume we just need to put the action within the if statement but I am not sure. I am going to go workout try it and then come back if I still don’t get the results I want.
Thanks again!
hey !thanks alot,but can u pls tell me how can i process multiple forms on 2 different pages.In other words dividing a simple form into 2 pages and then processing it.
Pls reply soon.
Sure. Just use Sessions to move the information to the second page.
thank you for te valuable information. how can i show two forms in one form based on a click from one of the forms. i.e:
i want to show one form with a button, after submitting that form another form will be displayed on the same page containg the information of that form being submitted.
To move information from one page to the next you can use
$_POST. Make sure your form hasmethod="post"set and then on the second form, display the information from the first form by usingas the value for the new input fields.
Here’s how to post two forms at once (to the same script):
http://tech.solin.eu/doku.php?id=webdev:submitting_multiple_forms_at_once
This solution relies on the javascript prototype library. There are also various jQuery solutions out there.
Onno
very nice explanation! which I search 3days along. thanks a lot…