Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Voting Poll with no buttons / submit button...

    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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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?

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    <a href="vote.php?v=1">VOTE YES</a>

    The script called "vote.php" ...
    PHP Code:
    <?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");

    ?>

    .

  • Users who have thanked mlseim for this post:

    prophecy (04-25-2012)

  • #5
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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:
    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:
    Code:
    <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 />
    Last edited by prophecy; 04-25-2012 at 04:18 PM.

  • #6
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    // Double post
    Last edited by prophecy; 04-25-2012 at 04:09 PM.

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,503
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The values from the form are references to which one they picked, not integers.

    The correct PHP (untested though) ...

    PHP Code:

    <?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.


    .
    Last edited by mlseim; 04-25-2012 at 04:48 PM.

  • #8
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yes, there's javascript, but I didn't think that would affect anything but the fading answers/results?

    Here's the javascript:
    Code:
    <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":
    Code:
    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
    Last edited by prophecy; 04-25-2012 at 05:09 PM.

  • #9
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <a  value="0" onclick="getVote(this.value)" style="cursor: pointer">
    Will not work. I need to use:
    Code:
    <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?
    Last edited by prophecy; 04-25-2012 at 06:35 PM.

  • #10
    New Coder
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fixed it by using a "Label".
    For those who want the code:

    HTML:
    Code:
    <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:
    Code:
    .label_radio input { position: absolute; left: -9999px; }


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •