views:

74

answers:

3

I need to create a pretty extensive form with PHP/MySQL with a lot of conditional logic that ultimately ends with submission to the MySQL database. So basically the user makes their first choice with a radio button (has to be because they are "choosing" pictures) and then gets taken to the next "page" of the form based on that submission. That submission ALSO has to be stored in a session or cookie because at the end of the form I will gather all of their input and populate a row in the database. What should my approach be? Right now I'm writing forms that store the POST data in a SESSION and then populate the database row at the end, but as soon as I introduce conditional logic from page to page, the sessions break and don't get sent. Any help would be greatly appreciated.

+1  A: 

I did exactly this with a 250 question psychological test system. Once you think it through, it's not too difficult. The solution I used was to output in stacked divs rather than worrying about doing individual pages and posting to session--it's cleaner. A submit button lives on every div, but it only submits on the last one--a function showsthe next div and hides the current div upon click in every other "page". Using variables and PHP math, I can set the test to come out in different combinations--such as 1 page of 250, 5 pages of 50, etc. Your most difficult logic goes into building the page with the proper number of questions and making sure the divs are effectively titled to work with your show/hide script. In my case, I use Jquery's show/hide functionality. Works like a charm.

Because this solution uses one page, you don't have to worry about values being "forgotten" when they go back or forth. In my case, I don't even allow the user to go back because I don't give them any UI to do so. A back click on the browser would give them a warning that they'll be leaving the page. However, you could use several different methods to allow them to go back including UI such as a button, or setting up the back functionality using the browser's back button. A huge side benefit is that loading is next to nothing, and page changes are instantaneous to the user.

bpeterson76
It's more difficult but I'd implement a way to move forward and backward between sections as well. This will allow people to go back and make a change to an answer. Unless you don't intend to let them do so.
sholsinger
In my case I couldn't let them go back, it was a technical requirement. However, going back would be as simple as a button that figured out the current div, subtracted one, then did a show/hide, just the opposite of the forward.
bpeterson76
can't use javascript, just php
Rob Bennet
Can't use javascript because of technical requirement or knowledge? It's really a very easy thing to implement, we could walk you through.
bpeterson76
I don't know javascript is my issue
Rob Bennet
It's a great time to learn! With JQuery installed, it's as easy as including jQuery and doing a $('#div').hide() where #div is the id of the div you're hiding. Piece of cake. This is a great resource to learn more: http://www.learningjquery.com/2006/09/basic-show-and-hide
bpeterson76
+1  A: 

Sounds like a classic "wizard" interface. A quick google search brought up this, but I'm not sure if it's any good.

http://www.phpclasses.org/browse/file/8308.html

Nikki9696
I don't want to use a wizard, I want to build this from scratch
Rob Bennet
+1  A: 

The advantage of saving all data to a single update at the end of the wizard is simplicity. The advantage of writing to the DB after each step is that the user can come back and finish the wizard at a later date - you're not tied to the current user session.

If you're inclined to go with the "insert at the end" approach I'd recommend bpeterson76's idea of using JS to show / hide sections on a singler phisical form - just make sure you've got control of the user's back button, otherwise the user could loose all of their answers.

Robin
I want to use PHP only and can't write to the database for each step...only at the end
Rob Bennet