Wednesday, January 4, 2012

How to add an online poll to your own website.

This post is based on a coursework we did in the 1st year in web technology. It was a group cousework where we have to make our own website.
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??

 Yes
 No
 Maybe
 Other



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!!!!



Monday, January 2, 2012

Hello world!! (for blogs)

HELLO UNIVERSE !!!!

As an undergraduate student in Information technology, my life lately is woven around the mist of knowledge. so initially i thought to share the things i'm learning, with the universe so that even i can get the lessons sorted out.

   
This is the first step of me, sharing my thoughts and opinions with the world, as I go through all my theoretical knowledge and the lessons i learnt throughout the finite loop of learning. Feel free to give your valuable comments and feed-backs which will surely be an inspiration.