Results 1 to 10 of 10

Thread: How to create AJAX Poll in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    300

    How to create AJAX Poll in PHP?

    Hi friends,
    Extremely thanks for helping me last time. Now I am having one more problem. I am designing a web page in which I want to create a Poll by which the users can poll their opinion. But I don't have enough idea about the AJAX poll. So I need your help very immediately. Please tell me how to create AJAX Poll in PHP? It would be very great if you provide me some coding regarding the same.

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: The getVote() Function in PHP?

    The getVote() Function would be very useful for creating a poll using the PHP. The purpose of this function is to execute, when “yes” or “no” is selected in the HTML form. The following are some points related to the getVote() :
    • It defines the url (filename) to send to the server.
    • It also adds a random number to prevent the server from using a cached file.
    • It adds a parameter (vote) to the url with the content of the input field.
    • Opens the XMLHTTP object with the given url.
    • Sends an HTTP request to the server.

  3. #3
    Join Date
    Mar 2008
    Posts
    672

    Re: How to create AJAX Poll in PHP?

    The HTML page contains a link to an external JavaScript, an HTML form, and a div element. The following example demonstrates the same :
    HTML Code:
    <html>
    <head>
    <script type="demo/javascript" src="poll.js"></script>
    </head>
    <body>
    
    <div id="poll">
    <h3>Do you like PHP and AJAX??</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>

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: JavaScript code for AJAX Poll in PHP?

    The stateChanged() and GetXmlHttpObject functions are almost the same. The following is the JavaScript code stored in the file "poll.js" :
    Code:
    var xmlhttp;
    
    function getVote(int)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="poll_vote.php";
    url=url+"?vote="+int;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
      if (xmlhttp.readyState==4)
      {
      document.getElementById("poll").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
      {
      objXMLHttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    return objXMLHttp;
    }

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to create AJAX Poll in PHP?

    The final step would be calling the server page by the JavaScript code is a simple PHP file called "poll_vote.php". The following code explains the same :
    PHP Code:
    <?php
    $vote 
    $_REQUEST['vote'];

    $filename "poll_result.txt";
    $content file($filename);

    $array explode("||"$content[0]);
    $yes $array[0];
    $no $array[1];

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

    $insertvote $yes."||".$no;
    $fp fopen($filename,"w");
    fputs($fp,$insertvote);
    fclose($fp);
    ?>

    <h2>Result:</h2>
    <table>
    <tr>
    <td>Yes:</td>
    <td>
    <img src="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>No:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*round($no/($no+$yes),2)); ?>'
    height='20'>
    <?php echo(100*round($no/($no+$yes),2)); ?>%
    </td>
    </tr>
    </table>

  6. #6
    Join Date
    Jun 2010
    Posts
    1

    Re: How to create AJAX Poll in PHP?

    If I want to add one more option (here only yes or no given) to the above given code what are changes required to do.

    Please help me to add more options to this code.

    Thanks in Advance.

  7. #7
    Join Date
    Jun 2010
    Posts
    3

    Re: How to create AJAX Poll in PHP?

    These information were really nice, I really like, and also appreciate your work, awesome work, I hope that this will really help those who really wanted to create Ajax poll in PHP, I think that people will really like this information, and appreciate your work.

  8. #8
    rosesteven23 Guest

    Re: How to create AJAX Poll in PHP?

    You have given nice information. This code help me a lot.
    I try this code it really nice and solve my problem.
    I am sure many people will get help from this code.

  9. #9
    scanner007 Guest

    Re: How to create AJAX Poll in PHP?

    When a user choose an option above, a function called "getVote()" is executed. The function is triggered by the "onclick" event:
    <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 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>
    </html>

    The getVote() function does the following:

    * Create an XMLHttpRequest object
    * Create the function to be executed when the server response is ready
    * Send the request off to a file on the server
    * Notice that a parameter (vote) is added to the URL (with the value of the yes or no option)

    The PHP File

    <?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="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>No:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*round($no/($no+$yes),2)); ?>'
    height='20'>
    <?php echo(100*round($no/($no+$yes),2)); ?>%
    </td>
    </tr>
    </table>

  10. #10
    Join Date
    Aug 2011
    Posts
    1

    PHP Poll using AJAX

    Hello There

    I come to know you about PHP Poll using AJAX.

    I have a url which can help you.

    I hope it is enough for you.

Similar Threads

  1. Computer Virus type poll
    By snacdaws in forum Polls & Voting
    Replies: 4
    Last Post: 21-03-2013, 05:48 PM
  2. The armor poll in Elder scrolls
    By ABRIELLE in forum Video Games
    Replies: 6
    Last Post: 06-06-2011, 11:02 PM
  3. jQuery AJAX vs. Adobe AIR AJAX Class
    By Agaev in forum Windows Software
    Replies: 5
    Last Post: 06-07-2010, 01:59 PM
  4. How to create a blog using AJAX techniques
    By Makayla-alex in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:14 PM
  5. Reliance Big Tv Public poll
    By Bigbang in forum India BroadBand
    Replies: 2
    Last Post: 11-09-2008, 03:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,723,669.07777 seconds with 17 queries