articles

Using AJAX Poll in PHP

Anonymous User9454 28-Sep-2011

It is the act of regularly retrieving data from a server to obtain near-live data. This is done through AJAX because it is very lightweight. I.e. AJAX polling script is used for getting user perception (vote result) about any research, Ajax is used to return the results from this poll to your browser.

Here I am demonstrating a simple example which elaborates how to use AJAX Poll in PHP.

Example:
Coding of ‘AjaxPoll.php’:
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>Ajax Poll</title>
        <script type="text/javascript">
            function getPoll(intVal) {
                if (window.XMLHttpRequest) {
                    xmlHttp=newXMLHttpRequest();
                }
                xmlHttp.onreadystatechange=function () {
                    if (xmlHttp.readyState==4) {
                        document.getElementById('pollResult').innerHTML= xmlHttp.responseText;
                    }
                }
                xmlHttp.open("GET", "GetPollResult.php?vote="+intVal, true);
                xmlHttp.send();
            }
   
 
        </script>
               
       
    </head>
    <body>
      
        <fieldset style="width:500px">
            <legend>Vote</legend>
            <table>
                <tr>
                    <td>
                        <form>
                            <p style="forecolor:Gray"><h4>Is mindstick.com is a good website to developer point of view?</h4> </p>
                        <input type="radio" name="pollAnswer1" value ="Answer1" onclick="getPoll(this.value)" ></input> Yes </br>
                         <input type="radio" name="pollAnswer1" value ="Answer2" onclick="getPoll(this.value)" ></input> No </br>
                          <input type="radio" name="pollAnswer1" value ="Answer3" onclick="getPoll(this.value)" ></input> I don't know </br>
                        </form>
                       
                    </td>
                </tr>
            </table>
            </br>
          
           
        </fieldset> 
        </br>
       <span id="pollResult"></span>
          
    </body>
</html>

 

Now, here I am creating simple research page through which we have to get user perception result.

Using AJAX Poll in PHP

Now user can select anyone option in given options, when user selects any option then server side poll script will be called and result graph will be appear in below.

Coding of ‘GetPollResult.php’:
<?php
// creatin connection
$con = mysql_connect("localhost", "root", "root");
// check connection will be created successfully or not
if(!$con)
{
    // error message displays at time connection failed
    die ("Connetionnot established");
}
// select database name
mysql_select_db("mysqldatabase",$con);
// retrieve page url value
$selVal= $_GET['vote'];
// check which option selected
if($selVal == "Answer1")
{
    // codingto be executed when yes option is selected
  // Selectcount of yes column
    $qry = "select * from mysqldatabse.tblpollresult";
    $qryResult= mysql_query($qry,$con);
    $rowVal= mysql_fetch_array($qryResult);
    $val1= $rowVal['Answer1'];
   // update yes column value
   $qry = "update mysqldatabse.tblpollresult set Answer1 =$val1+1";
   mysql_query($qry,$con);
  
}
else if($selVal == "Answer2")
{
    // coding to be executed when NO option is selected
    $qry = "select * from mysqldatabse.tblpollresult";
    $qryResult = mysql_query($qry,$con);
    $rowVal = mysql_fetch_array($qryResult);
    $val2 =$rowVal['Answer2'];
    // update no column value is selected
    $qry = "update mysqldatabse.tblpollresult set Answer2 =$val2+1";
    mysql_query($qry,$con);
}
else
{
    // code to be executed if I don't knowis selected
    $qry = "select * from mysqldatabse.tblpollresult";
    $qryResult= mysql_query($qry,$con);
    $rowVal= mysql_fetch_array($qryResult);  
    $val3 =$rowVal['Answer3'];
  // update don't know column value
    $qry = "update mysqldatabse.tblpollresult set Answer3 =$val3+1";
    mysql_query($qry,$con);
}
 
 
?>
<h2> Result Graph:</h2>
<table>
    <tr>
        <td>
            Yes:
        </td>
        <td>  <img src='C://Users//Sachindra//Desktop//poll.gif' width='<?php $con = mysql_connect("localhost", "root", "root"); if(!$con) {  die ("Connetion not established"); } $qry = "select * from mysqldatabse.tblpollresult"; $qryResult = mysql_query($qry,$con);
          $rowVal = mysql_fetch_array($qryResult);
          $val1= $rowVal['Answer1'];
          $val2 =$rowVal['Answer2'];
          $val3 =$rowVal['Answer3'];
        
          echo(100*round($val1/($val1+$val2+$val3),2));?>' height ='10px'></img>
            <?php  echo(100*round($val1/($val1+$val2+$val3),2)).'%' ;   ?>
        </td>
    </tr>
    <tr>
        <td>No:</td>
        <td>
        <img src='C://Users//Sachindra//Desktop//poll.gif' width='<?php $con = mysql_connect("localhost", "root", "root"); if(!$con) {  die ("Connetion not established"); } $qry = "select * from mysqldatabse.tblpollresult"; $qryResult = mysql_query($qry,$con);
          $rowVal = mysql_fetch_array($qryResult);
          $val1= $rowVal['Answer1'];
          $val2 =$rowVal['Answer2'];
          $val3 =$rowVal['Answer3'];
          echo(100*round($val2/($val1+$val2+$val3),2));?>' height ='10px'></img>
         <?php  echo(100*round($val2/($val1+$val2+$val3),2)).'%' ;   ?>
        </td>
    </tr>
    <tr>
        <td>
            Don't Know:
        </td>
        <td>
           <img src='C://Users//Sachindra//Desktop//poll.gif' width='<?php $con = mysql_connect("localhost", "root", "root"); if(!$con) {  die ("Connetion not established"); } $qry = "select * from mysqldatabse.tblpollresult"; $qryResult = mysql_query($qry,$con);
          $rowVal = mysql_fetch_array($qryResult);
          $val1= $rowVal['Answer1'];
          $val2 =$rowVal['Answer2'];
          $val3 =$rowVal['Answer3'];
          echo(100*round($val3/($val1+$val2+$val3),2));?>' height ='10px'></img>
            <?php  echo(100*round($val3/($val1+$val2+$val3),2)).'%' ;   ?>
        </td>
    </tr>
</table>
 
Output:

Using AJAX Poll in PHP



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By