prophecy 04-25-2012, 09:24 AM Hi guys,
I know this is probably really simple, but I couldn't find a voting poll that would work this way and have reached a brick wall.
I want to make a voting poll using <form> etc or whatever it is, but not have any "radio" buttons or "submit" button.
Pretty much just the text as a link. When the link is clicked, the vote is counted and simply displays a percentage on the right of that text (with no bars).
Very simple look. I'm wondering how I can do this?
Any help wold be appreciated.
mlseim 04-25-2012, 12:24 PM How are you saving the "votes"? With a database of some kind?
Can a person vote only once ever, or each time they visit the site?
If the vote is restricted by IP, what happens when a shared computer is used?
prophecy 04-25-2012, 12:56 PM I'm not too tech-savvy with php/javascript etc, so I'll most likely use a default method to saving the results, either within an XML file or text file.
People will be able to only vote once per day using a cookie-based system (this is what I've seen from a tutorial).
Let me make this perfectly simple, because I believe you're over complicating it. I have a poll, it works, it does whatever it needs to do. All I want is to find out how to remove the "radio" buttons and "submit" button and simply have a text link that users click on to vote.
Thanks for your input.
mlseim 04-25-2012, 03:23 PM <a href="vote.php?v=1">VOTE YES</a>
The script called "vote.php" ...
<?php
// default vote to zero
$vote=0;
// see if they clicked the link with the URL variable
if($_GET['v']){
// make sure the value is numeric, not alpha.
if (is_numeric($_GET['v'])) {
$vote=$_GET['v'];
}
}
// process the vote in whatever database you have
// $vote will either equal 0 or 1
// redirect back to the page (wherever that is) ...
header("location: index.php");
?>
.
prophecy 04-25-2012, 03:43 PM Thanks for this.. I'm still a bit lost though.
I found a different poll online that seems to work somewhat. Whenever I vote though, regardless of which one you vote, it seems to vote for $vote1.
Any idea what I did wrong?
Here is the code:
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$vote1 = $array[1];
$vote2 = $array[2];
$vote3 = $array[3];
$vote4 = $array[4];
$vote5 = $array[5];
if ($vote == 1)
{
$vote1 = $vote1 + 1;
}
if ($vote == 2)
{
$vote2 = $vote2 + 1;
}
if ($vote == 3)
{
$vote3 = $vote3 + 1;
}
if ($vote == 4)
{
$vote4 = $vote4 + 1;
}
if ($vote == 5)
{
$vote5 = $vote5 + 1;
}
//insert votes to txt file
$insertvote = $vote1."||".$vote2."||".$vote3."||".$vote4."||".$vote5;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Question?</h2>
<?php echo(100*round($vote1/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 1<br />
<?php echo(100*round($vote2/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 2<br />
<?php echo(100*round($vote3/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 3<br />
<?php echo(100*round($vote4/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 4<br />
<?php echo(100*round($vote5/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 5<br />
Here is the HTML:
<h2>Question?</h2>
<a value="vote1" onclick="getVote(this.value)" style="cursor: pointer"> Answer 1</a><br />
<a value="vote2" onclick="getVote(this.value)" style="cursor: pointer"> Answer 2</a><br />
<a value="vote3" onclick="getVote(this.value)" style="cursor: pointer"> Answer 3</a><br />
<a value="vote4" onclick="getVote(this.value)" style="cursor: pointer"> Answer 4</a><br />
<a value="vote5" onclick="getVote(this.value)" style="cursor: pointer"> Answer 5</a><br />
prophecy 04-25-2012, 03:53 PM // Double post
mlseim 04-25-2012, 04:45 PM The values from the form are references to which one they picked, not integers.
The correct PHP (untested though) ...
<?php
$select = $_REQUEST['vote'];
$vote=0;
if($select == "vote1"){
$vote=1;
}
if($select == "vote2"){
$vote=2;
}
if($select == "vote3"){
$vote=3;
}
if($select == "vote4"){
$vote=4;
}
if($select == "vote5"){
$vote=5;
}
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content);
$vote1 = $array[0];
$vote2 = $array[1];
$vote3 = $array[2];
$vote4 = $array[3];
$vote5 = $array[4];
if ($vote == 1)
{
$vote1 = $vote1 + 1;
}
if ($vote == 2)
{
$vote2 = $vote2 + 1;
}
if ($vote == 3)
{
$vote3 = $vote3 + 1;
}
if ($vote == 4)
{
$vote4 = $vote4 + 1;
}
if ($vote == 5)
{
$vote5 = $vote5 + 1;
}
//insert votes to txt file
$insertvote = $vote1."||".$vote2."||".$vote3."||".$vote4."||".$vote5;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Question?</h2>
<?php echo(100*round($vote1/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 1<br />
<?php echo(100*round($vote2/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 2<br />
<?php echo(100*round($vote3/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 3<br />
<?php echo(100*round($vote4/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 4<br />
<?php echo(100*round($vote5/($vote1+$vote2+$vote3+$vote4+$vote5),5)); ?>% | Answer 5<br />
EDIT:
Looks like there may be some javascripting also ... that might make my answer incorrect.
.
prophecy 04-25-2012, 04:54 PM Yes, there's javascript, but I didn't think that would affect anything but the fading answers/results?
Here's the javascript:
<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>
I will test your post now.
Edit: Tried it out and got this result under each "Answer":
Warning: Division by zero in /home/buffed/public_html/poll_vote.php on line 65
This is where I found the original poll script: http://www.w3schools.com/php/php_ajax_poll.asp
prophecy 04-25-2012, 06:33 PM Figured out the problem.
The link seems to be stuffing up the value function and as such, I have to use radio buttons in this feature.
Using:
<a value="0" onclick="getVote(this.value)" style="cursor: pointer">
Will not work. I need to use:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)" />
Which brings me back to my original question, is there another way to get rid of the radio button and simply have a link?
prophecy 04-25-2012, 07:40 PM Fixed it by using a "Label".
For those who want the code:
HTML:
<label class="label_radio" for="0" style="cursor: pointer">
<input type="radio" name="vote" value="0" onclick="getVote(this.value)" id="0" />Text</label>
CSS:
.label_radio input { position: absolute; left: -9999px; }
|
|