Overview

This project illustrates how to create a webpage that consists of a single file but which appears to the user to be multiple pages. The specific implementation is a market research study for an alternative religious organization. The advantage of creating the study this way is that the programmer can easily control which page the user can navigate to at a given time; the current page information is hidden in the POST variable.

In this particular survey, all questions are closed end, either check boxes or radio buttons. Also, there are no randomization, skippable questions, or other characteristics of a more robust study. Results are provided in the form of a single 42-character string that would be parsed into data management software, such as SPSS®.

The full code and the survey are available for review. The code is fairly straightforward, and is offered as a way of educating through example.

Hidden POST variables

The respondent's answers are stored in a single variable, $response. There are 42 digits needed with this particular survey; the number is low because all questions are closed-end radio (single punch) and checkbox (multi punch). Also, the radio questions only have six options, and hence only need a single column. Obviously, with open-ended responses, $response would need to be larger.

The variable $screenno holds the current screen to be displayed. Most questions on the survey are stretched over several screens to make it easier for the respondent.

Updating responses

The addresps function updates a single response value:

function addresps ($newval, $colnum) {
// Change a single column response (radio button)

    // Reset the current value
    $GLOBALS['response'] = substr_replace($GLOBALS['response'], 0, $colnum, 1);
        
    // Update value if appropriate 
    if (isset($_POST[$newval])) { 
        $GLOBALS['response'] = substr_replace($GLOBALS['response'], 
        $_POST[$newval], $colnum, 1); 
    }
}

$newval is a variable name matching the specific question that's being answered, based on the name attribute of the input tag. For instance, consider this code:

function radio1 ($radname, $colnum, $qval, $qlabel) {
// Print out a single radio button (as a stand-alone row)
    echo "<p class=\"indent\"><input type=\"radio\" name=\"${radname}\" value=\"${qval}\"";
    if (substr($GLOBALS['response'], $colnum, 1) == $qval) {
        echo " checked=\"checked\"";
    }
    echo "> ${qlabel}</p>";
}
...
radio1 ('q5', 41, 7, "I'm a guest");

This generates the HTML code (if "I'm a guest" is not currently checked):

<p class="indent"><input type="radio" name="q5" value="7"> "I'm a guest"</p>

$newval in addresps would then be q5.

$colnum represents the column of $response where this particular response appears. Note that if two digits were used (for instance, for a ten-point scale), these functions would need to be adjusted appropriately. (Also note, however, that 10-point scales are sometimes coded into a single column with non-numerics as needed, such as * for 10 and - for Don't Know.)

Switch/Case

Having multiple pages generated by a single code file is accomplished by using the $screenno variable in conjunction with two switch/case structures. The first one updates the $response variable based on the user's actions. The second one creates the appropriate code for each question.

In actual implementation, you could either program it to save responses as pages are turned, or to wait until the last screen and then either update a database or email responses in the last case, replacing this line:

qtext ("The responses were coded as: ".$response);

with appropriate code.