To allow users to vote on our poll we need to setup a form.
This script will be much like the script to output the results, except this
time, instead of having the for loop echo the results for each option, we will have it echo
a radio button for each one.
<?php
|
|
Ok, that's the script to setup the form. Basically, it just grabs all the information we need first, like in results.php. After we have that information, we output a form, with the action as $self, which holds the path to the file it is called from, and we give it a post method.
Then we just loop through the options and echo out the option and a radio button. Next we write out some submit buttons, one to submit the vote, and another to view the results.
If you test that script, you'll see that clicking either submit button doesn't do anything, we still have to setup that part. To accomplish this we'll use the isset() function to see if either submit button has been clicked. Below is what the final vote.php script looks like.
<?php
|
|
Line 19 checks whether or not either submit button has been clicked. If not, it outputs the form. If so, lines 40-52 are executed. Line 42 checks if the submit button the user clicked was the 'Vote' button. If so, the script increments the vote count for that option by one, writes the poll data to the file, then displays the results by included the results.php file with require(). Otherwise, the the 'View' button has been clicked, so the script includes the results.php file without incrementing the vote count.
Thats it, the poll is complete. The users can now view the results and vote in the poll. To create a new poll, all you have to do is edit the $poll array in the setup.php file, then run the script again.
With a little more effort you can setup the script to handle multiple polls and maybe even comments, like
the ones here at ronsguide.com. That's all for this tutorial, hope it was helpful to you. 
| Discuss Tutorial: Flat File Poll | 93 Comments |

Gotta love the way you've set it out, Ron. 


im happy now
it will be on my portfolio is that ok

I just have a small problem...the poll shows up twice!
How do I get it to show up only once? 
