Processing Multiple Forms on One Page with PHP

Posted on May 4, 2009  |  Category: Tutorials

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.


Tags: , , , , , , , , , , , ,

Short URL: http://bavotasan.com/?p=567

8 Responses to “Processing Multiple Forms on One Page with PHP”

  1. Very nicely explained & a useful tip – many thanks!

    Dez.

    #2227
  2. Still Lost

    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!

    #3042
  3. 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.

    #3444
  4. Howida

    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.

    #5527
    • To move information from one page to the next you can use $_POST. Make sure your form has method="post" set and then on the second form, display the information from the first form by using

      <?php echo $_POST['input-name']; ?>

      as the value for the new input fields.

      #5537
  5. Onno

    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

    #6225
  6. very nice explanation! which I search 3days along. thanks a lot…

    #6354

Leave a Reply

To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.