Question

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?

Was it helpful?

Solution

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..*

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top