when adding a poll to a website the rule of thumb is to give the user the results of the poll without making him/her to reload the whole page.AJAX allows you to do the exact same.lets look at an AJAX poll which is done by using PHP. And adding to that you can also restrict the user from voting more than once, using cookies. We'll get to that later.
AJAX
AJAX stands for "Asynchronous JavaScript and XML".It allows us to reload a part of the web page without reloading the whole page. We can exchange the data with the server and update the part we want to update in our web page. So AJAX technique is ideal for an online poll. We can reload the poll with the results, without reloading the whole page.
And we can set a cookie so that the user can't vote more than once. setting a cookie is so easy. We can do it in a single line.
setcookie("voted", 1);From that line we are setting a cookie named "voted" in the browser itself.And form the action button we can check for that cookie from another separate php file and then restrict the user accordingly.
code for the page file.(We are checking the cookie when the user is voting.And if the cookie is there, the user is given the message "You Have Already Voted!")
poll_vote1.php
DO YOU BELIEVE IN 2012??
And all the logical calculation goes in another php file. We calculate the parentage of votes in a separate php file. The results will be displayed like this in the web page,without reloading the whole page.
poll_vote.php
RESULTS
Yes :
%
No :
%
Maybe:
%
Other :
%
Now we have to save a text file to get the results.Name the file as "poll_result.txt". That text file will be updated as soon as a user votes. all the votes will be stored separated by "||" inside the text file.And it is done by this following piece of code.
$insertvote = $ans1."||".$ans2."||".$ans3."||".$ans4;$fp = fopen($filename,"w");fputs($fp,$insertvote);fclose($fp);Hope you have learnt something going through this post.And if you have any questions feel free to ask.
Meanwhile enjoy coding!!!!



good stuff machan!! keep them coming!! :)
ReplyDeletewell done machn!!
ReplyDeletekeep this good work up!!! :)