I am playing around with creating a multipage survey and found a resource on the web that uses $stage to pass hidden variables in order to get the data values through subsequent pages.
I understand this but am having the following problem:
The example file has a submit button on the first question that opens a new window for the second set of questions. I want to be able to have the user answer the questions then hit a next link that will open the next set of questions within the existing window.
Ultimately I will dump the results into a unique data file for the survey taker, results to be correlated later.
I made sure to search for an example of this all day yesterday before pestering the forum but came up empty.
The sample code I am building from is listed below:
<?php
//first function grabs name
function display_name() {
global $PHP_SELF;
?>
<FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=GET>
Name: <INPUT TYPE=TEXT NAME="name"><BR>
<INPUT TYPE=HIDDEN NAME="stage" VALUE="cheese">
<INPUT TYPE=SUBMIT VALUE="Thanks!">
</FORM>
<?php
}
?>
<?php
//second function
function display_cheese() {
global $PHP_SELF;
global $name;
?>
<FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=GET>
Favorite Cheese: <INPUT TYPE=RADIO NAME="cheese" VALUE="brie">Very soft French Brie
<INPUT TYPE=RADIO NAME="cheese" VALUE="cheddar">Farmhouse English Cheddar
<INPUT TYPE=RADIO NAME="cheese" VALUE="mozzarella">Italian Buffalo Mozzarella
Favorite Times to Eat Cheese: <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="m">Morning
<INPUT TYPE=CHECKBOX NAME="times[]" VALUE="n">Noon
<INPUT TYPE=CHECKBOX NAME="times[]" VALUE="d">Dinner
<INPUT TYPE=CHECKBOX NAME="times[]" VALUE="l">Late night
<INPUT TYPE=HIDDEN NAME="name" VALUE="<?php echo htmlspecialchars($name); ?>">
<INPUT TYPE=HIDDEN NAME="stage" VALUE="results">
<INPUT TYPE=SUBMIT VALUE="Thanks!">
</FORM>
<?php
}
?>
<?php
//process form is the same
function process_form() {
global $name;
global $cheese;
global $times;
if ($cheese == 'brie') { $cheese_message = 'I love brie.'.'<br>'; }
elseif ($cheese == 'cheddar') { $cheese_message = 'Cheddar is awesome!'.'<br>'; }
else { $cheese_message = 'Fresh mozzarella is divine.'.'<br>'; }
$favorite_times = count($times);
if ($favorite_times <= 1) {
$times_message = 'You should eat cheese more often.'.'<br>';
} elseif ($favorite_times > 1 && $favorite_times < 4) {
$times_message = 'Those are good times to eat cheese.'.'<br>';
} else {
$times_message = 'You are eating too much cheese.'.'<br>';
}
echo "Hello $name.".'<br>';
echo "$cheese_message $times_message";
}
?>
<?php
//page display logic
if (empty($stage)) { display_name(); }
elseif ($stage == 'cheese') { display_cheese(); }
else { process_form(); }
?>
Any help for this newbie would be great.
Tony