1. Home
  2. Computing & Technology
  3. PHP / MySQL

Simple PHP & MySQL Poll

By Angela Bradley, About.com

4 of 5

Voting Script - Part 3

//if they are not voting, this displays the results if they have already voted
if(isset($_COOKIE[$cookie]))
{
pie ();
}

// or if they have not voted yet, they get the voting box
else
{
if(!$mode=='voted')
{
?>
<form action = "<?php echo $_SERVER[’PHP_SELF’]; ?>" method = "GET">
<select name="vote"> <option value="1">Option 1</option>
<option value="2">Option 2</option> <option value="3">Option 3</option>
</select>
<input type=hidden name=mode value=voted>
<input type=submit>
</form>
<?
}
}
?>
The final part of the script runs if they are not in voting mode. It checks to see if they have a cookie in their browser. If they do, then it knows they have already voted and displays the poll results for them. If there is no cookie, it then checks to make sure they aren't in voted mode. If they are, then nothing happen, but if they are not it displays the form that lets them vote.

It is a good idea to include this poll on your page using the include function. Then you can place the poll anywhere you want within the page, simply using one line.

INCLUDE 'http://www.yoursite.com/path/to/poll.php' ;

4 of 5

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. Simple PHP Poll - Graphical PHP Poll - PHP Poll Tutorial

©2008 About.com, a part of The New York Times Company.

All rights reserved.