views:

52

answers:

4

How do I create a form that spans over multiple pages? I would like to ask a large amount of questions, and based on the answers of previous questions, new specific questions will be asked.

+1  A: 

There are several ways to do this:

  • Incrementally populate a database, submit by submit
  • Keep all form fields on every page, but hide (<input type='hidden' ...>) the ones that shouldn't show up (as @Trufa suggests in another answer)
  • Accumulate an object that's persisted implicitly in the session (depends on your server architecture)
  • Accumulate stuff in one or more cookies (seems icky by I list it for completeness)

If you wanted to be super-trendy, you could accumulate information in HTML5 client-side storage. Of course that'd rule out older Microsoft browsers.

Pointy
A: 

There isn't such a thing as a single form spanning multiple pages: each page/form is independent of the others.

If you somehow keep track of what has already been entered (possibly in session variables), then you can just display the new questions in a new form until you have all the data you want.

casablanca
+4  A: 

Traditional method: store all the previous answers to questions in <input type="hidden"> fields on subsequent pages, so that the last form contains the entire submission (only not all visible). Drawback: file uploads are annoying to remember, amount of data can get large.

Common method: throw answers-so-far into the session or cookies as you go along, picking them out at the end. Drawback: concurrent operations end up with unpredictable conflicting results. File uploads still annoying. I would avoid cookie/session storage solutions for this.

Modern method: have a simple (long) single form, but use JavaScript to show only part of it at once. On clicking ‘Next’ you simply hide the previous page worth of content and show the next. The last button is the real submit button.

bobince
+1 Modern method should be emphasized. :)
galambalazs
+1 Common method. The modern one is "cool" but it's heavy on the client..
Victor Z.
A: 

I'm assuming that by your question, as there is no such thing as a multiple page form, you mean how do you build up a set of answers from a user by using multiple pages, each containing a form.

The mechanism PHP supplies for such things is the session mechanism. You'd build up the set of answers in the session as the user submits each page, and when you have a complete set the full set of answers in the session gets committed to the database.

This isn't the only approach of course, you could also incrementally add the data to the database on each page submit, but then you will end up with partial answer sets where people have only done the first page or first few pages and then abandoned it. You'd have to write code to handle these partial submissions.

Gordon