Skip to content

Multiple Forms on One Page

Sometimes you need multiple forms on a page, like an Account Settings page where a user can change their profile information, password, avatar, etc. You don't really want the Change Password form to be part of the Profile form, and you definitely don't want the Avatar form to be part of the Change Password form.

The solution? Multiple forms, and it's easier than ever with Formr.

Step 1: Instantiate the Forms.

The first step is to instantiate (create) your forms, and give each its own name and ID.

$profileForm = new Formr\Formr;
$profileForm->id = 'UserProfile';

$passwordForm = new Formr\Formr;
$passwordForm->id = 'ChangePassword';

Step 2: Build the forms.

The next step is to build the forms. Build them the short way, or the long way; doesn't matter.

$profileForm->create_form('Name, Email, Bio');
$passwordForm->password('password', 'Password');
$passwordForm->password('confirm_password', 'Confirm Password');
$passwordForm->submit_button('Change Password');

Step 3: Submit the Forms.

Finally we can check if each form was submitted, and then process the data for that form. The only catch here is that you will need to pass the form's ID to the submitted method so Formr knows which form has been submitted. You can pass the id variable or a string containing the form's ID.

if($profileForm->submitted($profileForm->id)) {
    // ProfileForm was submitted
if($passwordForm->submitted('ChangePassword')) {
    // ChangePassword was submitted

Full Example

# include Formr
require 'Formr/class.formr.php';

# instantiate the User Profile form
$profileForm = new Formr\Formr();
$profileForm->id = 'UserProfile';

# instantiate the Change Password form
$passwordForm = new Formr\Formr();
$passwordForm->id = 'ChangePassword';

# check if the User Profile form was submitted
   $data = $profileForm->validate('Name, Email, Bio');

   if($profileForm->ok()) {
      $profileForm->success_message('Profile Updated!');

# check if the Change Password form was submitted
   # this time we're using the post() method for validating the password field
   $password = $passwordForm->post('password, 'Password', matches[confirm_password]');

   if($passwordForm->ok()) {
       $passwordForm->success_message('Password Changed!');
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">

        # display messages for each form

    <!-- build the forms -->

    <h2>Account Profile</h2>
    <?php $profileForm->create_form('Name, Email, Bio|textarea'); ?>

    <h2>Change Password</h2>
    <?php $passwordForm->create_form('Password, Confirm Password'); ?>