InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Working with Multipage Forms in PHP
PHP

Working with Multipage Forms in PHP

InfinityCoder November 29, 2016

You want to use a form that displays more than one page and preserves data from one page to the next.

For example, your form is for a survey that has too many questions to put them all on one page.

Use session tracking to store form information for each stage as well as a variable to keep track of what stage to display.

Example 9-21 displays the four files for a two pageform and showing the collected results.
Example 9-21. Making a multipage form
The “deciding what to do” logic (stage.php):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Turn on sessions
session_start();
 
// Figure out what stage to use
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (! isset($_POST['stage']))) {
    $stage = 1;
} else {
    $stage = (int) $_POST['stage'];
}
 
// Make sure stage isn't too big or too small
$stage = max($stage, 1);
$stage = min($stage, 3);
 
// Save any submitted data
if ($stage > 1) {
    foreach ($_POST as $key => $value) {
        $_SESSION[$key] = $value;
    }
}
 
include __DIR__ . "/stage-$stage.php";

The first page of the form (stage-1.php):

1
2
3
4
5
6
7
8
<form action='<?= htmlentities($_SERVER['SCRIPT_NAME']) ?>' method='post'>
 
Name: <input type='text' name='name'/> <br/>
Age: <input type='text' name='age'/> <br/>
 
<input type='hidden' name='stage' value='<?= $stage + 1 ?>'/>
<input type='submit' value='Next'/>
</form>

The second page of the form (stage-2.php):

1
2
3
4
5
6
7
<form action='<?= htmlentities($_SERVER['SCRIPT_NAME']) ?>' method='post'>
 
Favorite Color: <input type='text' name='color'/> <br/>
Favorite Food: <input type='text' name='food'/> <br/>
 
<input type='hidden' name='stage' value='<?= $stage + 1 ?>'/>
<input type='submit' value='Done'/>

The displaying-results page (stage-3.php):

1
2
3
4
Hello <?= htmlentities($_SESSION['name']) ?>.
You are <?= htmlentities($_SESSION['age']) ?> years old.
Your favorite color is <?= htmlentities($_SESSION['color']) ?>
and your favorite food is <?= htmlentities($_SESSION['food']) ?>.

At the beginning of each stage in Example 9-21, all the submitted form variables are copied into $_SESSION.

This makes them available on subsequent requests, including the code that runs in stage 3, which displays everything that’s been saved.
PHP’s sessions are perfect for this kind of task since all of the data in a session is stored on the server.

This keeps each request small—no need to resubmit stuff that’s been entered on a previous stage—and reduces the validation overhead.

You only have to validate each piece of submitted data when it’s submitted.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Localizing Included Files in PHP
You want to include locale-specific files in your pages. Modify …

Localizing Included Files in PHP

Creating a Dynamic Variable Name in PHP
You want to construct a variable’s name dynamically. For example, …

Creating a Dynamic Variable Name in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap