Presenting the Survey
As the survey is entirely defined in a database, presenting the data can be a straightforward data transformation: You'll write code to read the question text, read the answer options for that question, and then render them in HTML. In the download for this article, you'll find this complete file (survey.php
); in the following paragraphs I'll walk through each section in the file and show how it contributes to the rendering of the survey.
You first need to retrieve the question text and options. The correct survey will be accessed using a parameter that is passed into the page when it is called. In other words, to access survey No. 1, you call the page using http://yourserver/survey.php?questionid=1 (see Listing 1).
Now that your code has the information pertaining to QuestionID 1, the next step is to draw this information and render it in a browser (see Figure 3). How the application does this is very simple. It takes an input parameter using the PHP $_GET command, which it uses to build a SQL query to select the question text from the Questions table. A second SQL query uses the same parameter to get a recordset of the options from the Options table.
|Figure 3. The Color Question: Here is our sample survey question in action.|
Now that you have the display information, you simply need to generate an HTML page to display them. Because site visitors need to interact with the page, an HTML form is used. The form will display the options in a 'radio' control; when the user makes a selection, the form is submitted to another script, which you will see in the next section. The PHP that generates the HTML for this form is seen in Listing 2
. The completed form is shown in Figure 3
You can see that the while loop in Listing 2 outputs the HTML for <INPUT> controls of type 'radio.' These are given a value that increments from zero. So the value for the first option is 0, for the second it is 1, for the third it is 2, etc. You can easily expand this to custom values by adding the custom value to the options table, adding that column to the options query in Listing 1 and then outputting it to the HTML. For the sake of simplicity, a hard count from zero is used in this example. This forms-submittal script is 'process.php,' which you will see in the next section.
Processing the Response
The submission script, which is defined in the form as process.php, will receive the results of the form as $_POST variables. You are only interested in two of these variables. First is the value of the user's selection from the options. In HTML every radio button has a name and a value. If you give all your options the same name (in this case 'Q'), the $_POST variable for that name will contain the value of the selected radio button.
The second variable you're interested in is the value of the question for which the answer is being submitted. This is stored in the form (Listing 2) as a hidden field called QID. The code snippet below handles QID.
$QID = $_POST['QID'];
die("Can only be called from the survey page");
$ANSVAL = $_POST['Q'];
| Author's Note: For this example, all answers are written, with no validation to see if somebody has voted twice. In the download (process.php), there is a section of code (that is commented out) that queries the IP address of the voter to see if they have already answered this question. If they have, then their vote is rejected. If you want to use this functionality, just remove the comments. (You can get the users IP address using the command GETENV("REMOTE_ADDR").
With these variables in hand, the next step is to write the new 'answer' data to the database. Writing the result to the database is very straightforward, just using a SQL INSERT query:
INSERT INTO ANSWERS (AnswerValue, AnswerIP, QuestionID) VALUES (x,y,z)
Where x, y, and z are the appropriate values for the answer, the IP address, and the ID of the question. Here is the PHP that process the query:
"INSERT INTO ANSWERS (AnswerValue,AnswerIP,QuestionID) VALUES (";
$SQL = $SQL . $ANSVAL . ",'";
$SQL = $SQL . $ANSIP . "',";
$SQL = $SQL . $QID . ")";
$result = mysql_query($SQL) or die ("SQL Error: " . $SQL);