Jump to content

Recommended Posts

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("Microsofticon1.png.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

Link to comment
https://forums.phpfreaks.com/topic/279332-php-poll/
Share on other sites

It is a very good practice to wrap all code in [ code ] [ /code ] tags (without the spaces so that it gets parsed correctly). Inline code which doesn't take up much space can be wrapped in [ ic ] [ /ic ] tags (again, without the spaces so that it gets parsed).

 

Examples.

 

 

// this is a demonstration of the code tag
// it features a few comments to make its purpose clear

 

$ic = 'this is some short inline code';

Link to comment
https://forums.phpfreaks.com/topic/279332-php-poll/#findComment-1436854
Share on other sites

Hello,, Ill try to be as specific as i can,, I am trying to use the code above as i have learned from w3 schools and i am trying to figure out how i can add a text link on my poll to linnk to the results without actualy adding another value to the total amount of votes.. Just a link to the results. I am a new programmer and i love trying to learn but as you know examples are very helpfull. Any help is greatly appreciated

Link to comment
https://forums.phpfreaks.com/topic/279332-php-poll/#findComment-1437802
Share on other sites

It is a very good practice to wrap all code in [ code ] [ /code ] tags (without the spaces so that it gets parsed correctly). Inline code which doesn't take up much space can be wrapped in [ ic ] [ /ic ] tags (again, without the spaces so that it gets parsed).

 

Examples.

// this is a demonstration of the code tag
// it features a few comments to make its purpose clear

$ic = 'this is some short inline code';

meat head

Link to comment
https://forums.phpfreaks.com/topic/279332-php-poll/#findComment-1437902
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.