Write a comment on this article
With a poll you can get your visitor's opinions on a specific topic. This makes your website more interesting and interactive. Let's see how we can implement such a poll.
We will need two files. One to store the votes (votes.txt) and one to read and write the results (index.php). It's very important that the file votes.txt is writable by the HTTP server (adjust security on Windows system or use the chmod command on Unix systems). Moreover, the votes.txt must contain one row with a 0 for each possible answer. In this tutorial we will use 5 possible answers, so the votes.txt will look like this:
0 0 0 0 0
To the programming part: first we create an array which contains the possible answers and initialize the variable $total to store the total number of votes.
$answers = array("white","yellow","blue","red","green");
$total = 0;
Now we print a form with the question, the possible answers and a submit button. As we store the answers in an array, we can use a for-loop to print them. Also, we print a header for the results:
<h3>What is your favorite color?</h3>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><input type="radio" name="vote" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?><br /><?php
}
?>
<p><input type="submit" value="Vote!"></p>
</form>
</p>
<h3>Results</h3>
<p>
Here comes the interesting part: the voting. We read the file votes.txt completely into the array $votes using the function file. This way we have all results and can work with them.
When the form is submitted, the answers which has been chosen by the visitor is passed with the parameter "vote". So we read this parameter.
$votes = file("votes.txt");
$vote = $_POST["vote"];
If the form has been submitted, we increment the chosen answer:
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
Now that we have the updated results stored in $votes, we write them back into the results file. First, we open the file with write-access:
$handle = fopen("votes.txt","w");
In a foreach-loop we write the data and sum up the total votes which are stored in $total. Using the function chop we strip whitespaces at the end of each value.
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
And the careful programmer knows: an open file handle has to be closed again:
fclose($handle);
Now we have updated and stored the results, it's time to print them. We can use a for-loop again:
//print votes
for($i=0;$i<count($answers);$i++){
echo "{$votes[$i]} {$answers[$i]}<br />";
}
Finally, we print the total votes which have been calculated before and finish our little script:
</p> <p>Total: <?php echo $total; ?> votes.</p>
You can try this script here: demos/Easy_poll/index.php. Or download it here: Easy_poll_1-0.zip
PHP, tutorial, poll, vote
If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.