1

I need to develop a form that spans multiple pages, but requires just one PHP script. On the first page, visitors are presented with a series of checkboxes. Depending on their choice(s), additional pages must be displayed in a specific order and then to a closing page. Each page contains a hidden input identifying what page number it is. What I have so far is this:

if(!isset($currentPage)) {
  echo $firstPage;
}
else {
  echo nextPage($currentPage);
}

function nextPage($lastPage) {
  switch($lastPage) {
    case '0':
      if(isset($opt1)) {
        return $page1;
      }
      else if(isset($opt2)) {
        return $page2;
      }
    case '2':
      if(isset($opt3)) {
        return $page3;
      }
    case '3':
    case '1':
      return $page4;
  }
}

The above code properly identifies whether to display page1, page2 or page3 after the starting page, but after that, no matter what other choices may have been selected, it always directs the visitor to the closing page (page4). What am I missing? Or have I gone about this all wrong?

Bryan
  • 849
  • 1
  • 7
  • 12
  • FYI: PHP _does_ have an [`elseif` keyword](http://php.net/manual/en/control-structures.elseif.php). There's no difference between using `elseif` and `else if` in your case, but if you use a colon to define your statement groups instead of braces, using `else if` will cause an error. – Lèse majesté Jan 28 '12 at 23:19
  • Where does `$currentPage` come from? Is it a variable always 3 after completion? – Wrikken Jan 28 '12 at 23:41
  • $currentPage is the variable derived from the hidden input on all pages. Sorry I forgot to mention that. @Lèsemajesté - I know there is an `elseif` but unless it is disrupting the function of the script, I prefer `else if` for readability. Personal preference I guess. – Bryan Jan 28 '12 at 23:45

1 Answers1

1

I would suggest storing answers in a session variable until the user finishes the survery.

So put

session_start();

on top of the script before outputing any text

and then whenever a user submits a form, goes to next page do this:

$_SESSION['answers'] = array_merge($_POST, $_SESSION['answers']);

This would store answers to one array, which you can then use when user finishes the survey.

*hint: since it's using array merge make sure the radio buttons on different pages have different names. Like: question_1, question_2..*

Tadej Magajna
  • 2,765
  • 1
  • 25
  • 41
  • I'm not worried about storing the answers just yet, it's progressing through pages that concerns me right now. Page flow does not function as I would like. But I will admit, that is good to know. – Bryan Jan 28 '12 at 23:52
  • I rescind my comment. I failed to consider that because I had not carried the option list through to each page, the script had no knowledge of what options had been checked at the start. Thanks for giving me the information I needed to realize what exactly I had done wrong. – Bryan Jan 29 '12 at 19:55
  • Although I completly misread your question, I'm still happy it helped =) – Tadej Magajna Jan 30 '12 at 05:33