Hello. I have been trying to get my php poll to show results if user has already voted, I have been trying for 2 months now and every thing i try doesnt work so i need help please, Can any one help me with this? Ill post what i have scripted so far
The poll.php
//<?php
?>
<html>
<head>
<script>
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","mypoll/poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<h3>Do you like PHP and AJAX so far?</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>
And then the poll_vote.php
//<?php
setcookie("poll","1",time()+86400*30);
if($_COOKIE['poll'] == 1)
{
die ("you've already voted in this poll");
}
ELSE
{
echo 'Thank you for your vote';
}
$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 += 1;
}
if ($vote == 1)
{
$no += 1;
}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Results: Do you prefer using contractor or do it your self?</h2>
<table>
<tr>
<td>contractor:</td>
<td>
<img src="mypoll/poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>myself:</td>
<td>
<img src="mypoll/poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
<td>Number of votes:</td>
<td>
<?php echo ($yes+$no); ?>
</td>
</tr>
</table>
Thank you for any help can give, Creig
↧
PHP Poll
↧