How the PHP Survey Works

 

This lesson is part of an ongoing Survey/Poll tutorial. The first part is here: Build your own Survey/Poll, along with all the files you need.

In the previous lesson, you set up the database for the PHP survey app. In this lesson, you'll see how it all works.

Have a look at the files for the survey. The files in the scripts/survey folder that you downloaded are these:

survey.php
setSurvey.php
setQuestion.php
process.php
viewResults.php
red.jpg

Start your server and take a look at how everything works. The first page to examine is the survey.php file. Once you've copied over all the files correctly, the address on your localhost for the survey.php file should be this:

http://localhost/survey/survey.php

And the page itself should look like this:

The PHP survey page

So there is a survey question, and three possible choices. The user would select one of the choices and then click the button to vote. There is also a button to view the results. This survey could go in a side bar on your web site.

Click the View Results button and you'll see this page (viewResults.php):

PHP survey - View Results

The question is repeated at the top, and the choices are there on the left. There also values for the percentages of votes, as well as the actual number who voted. (You could put all this in a HTML table, so that everything is neatly aligned.)

Go back to the survey page and you'll notice that same question appears. This is the default question. How, then, do you change the question?

Load up the page called setSurvey.php and you'll see this page:

Setting a question for the survey

Click the dropdown box to see more questions:

A HTML dropdown list filled from a database

These questions are being pulled from the survey database. They are then added to a HTML dropdown list. Select a question from the list and then click the button to set that question as your survey. You will then be taken back to the survey page, where you will see the question displayed:

Changing the question for your survey

The first thing you'll want to do when testing this page is to open up the code for the process.php page. Locate the following line:

$_SESSION['hasVoted'] = '1';

Comment this line out, otherwise you'll get the message "You've already voted" whenever you try to add a new vote to your database. (Don't forget to uncomment the line, if it's going onto your website.) You'll see how the process page works a little later.

To set new questions in the survey database, load up setQuestion.php. You'll see this:

Add a new question to the database

Type in a new question in the textbox at the top, and the three options in the textboxes below that. To add the question to the database, click the Set this Question button.

The other two files in the scripts folder are process.php and red.jpg. The process page is called from the survey.php file and is used to add a vote to the database. It also detects if the user has already voted, and displays a suitable message. You'll see how all this works during these tutorials.

The red.jpg file is used on the viewResults page. It's the horizontal bar that tells the user what percentage of voters selected a particular option. You can replace this red bar with anything you like.

 

In the next lesson, you'll take a look at the PHP code to set a question for the survey.