How to make polls for your website (PHP)

dsigamemaster11

New Member
Messages
1
Reaction score
0
Points
0
Hello. Today i'm gonna show you how to make polls for your website. NOTE: Only one question and two awencer picks in this polls!
Polls.php
FIrst off, make your file called "polls.php" Once you have that, then put this peace of code in.
HTML:
<html>
<head>
<script type="text/javascript">
function getVote(int)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("poll").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">
<h3>Do you like x10hosting?
</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)" />
<br />No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)" />
</form>
</div>

</body>
</html>
Then once you have done that, then make "poll_vote.php". Poll vote will be what will show the persentage of the polls. Once you do that, put this peace of code in,
PHP:
<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="http://upload.wikimedia.org/wikipedia/en/1/1c/Red_line.PNG"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="http://upload.wikimedia.org/wikipedia/en/1/1c/Red_line.PNG"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
NOTE: You are allowed to change the img src..
One last set from compleating the polls, make a file called poll_result.txt and thin put this in it "0||0" The first number represents the "Yes" votes, the second number represents the "No" votes. Hope it helped. Buh bye.bye.http://x10hosting.com/forums/images/smilies/smile.png
 
Top