Php Ajax Poll

Are you looking to create an interactive poll on your website using Php Ajax Poll?

With the help of Ajax, you can easily create a polling system that allows users to vote without reloading the page. In this article, we’ll walk you through the steps to create a PHP Ajax Poll.

Before we get started, let’s discuss what a PHP Ajax Poll is. Essentially, it’s a polling system that allows users to vote on a question or topic using Ajax, which stands for Asynchronous JavaScript and XML. Ajax allows the website to send and receive data from the server without having to refresh the page. This makes the polling system more user-friendly and interactive.

Throughout this course, we will cover the basics of AJAX and guide you through setting up a PHP backend to handle poll requests. We’ll also provide a simple poll example that you can customize to fit your specific needs.

Steps to Create a PHP Ajax Poll

To create a PHP Ajax Poll, you’ll need to follow these steps:

Step 1: Set up the HTML structure

The first step is to create the HTML structure for the poll. You can use a simple form with radio buttons for the options, and a submit button to submit the form.

You’ll also need to create a div to display the results of the poll.

Step 2: Create the database table

Next, create a database table to store the poll data.

The table should have columns for the poll question, the poll options, and the number of votes for each option.

Step 3: Create the PHP script

Create a PHP script that retrieves the poll data from the database and displays it in the poll form.

When the user submits the form, the script should update the database with the user’s vote and refresh the poll results using Ajax.

Step 4: Add Ajax functionality

Add Ajax functionality to the poll by using JavaScript and jQuery.

You’ll need to use the jQuery.ajax() method to send and receive data from the server without reloading the page.

You can use this method to update the poll results after the user submits their vote.

Step 5: Style the poll

Finally, style the poll using CSS to make it look attractive and user-friendly.

Here is an example of a poll that is able to show the results without the user having to reload the page.

Creating a PHP AJAX Poll

To create a PHP-AJAX poll, we will need to use a combination of PHP, AJAX, and HTML.

The poll will consist of a question and several answer choices.

When a user selects an answer choice, the poll will be updated with the results of the poll so far.

Whenever a user selects the option above, a function called getUserVote() will be executed in order to determine the user’s vote.

As soon as the event onclick is triggered, the function is invoked:


<html><head> <script> function getUserVote(int) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 & this.status == 200) { document.getElementById("poll-input").innerHTML = this.responseText; } }"GET", "getPollVotes.php?resp=" + int, true); xmlhttp.send(); } </script> </head><body> <div id="poll-input"> <h3>Have you enjoyed learning PHP and AJAX from Mr Examples?</h3> <form> <b>Yes!</b> <input type="radio" name="resp" value="0" onclick="getUserVote(this.value)"> <br> <b>No!</b> <input type="radio" name="resp" value="1" onclick="getUserVote(this.value)"> </form> </div> </body></html>
A user’s vote can be retrieved using the getUserVote() function in the following way:
  • Create a new instance of XMLHttpRequest.
  • As soon as the server response is ready, create the function that will be used to execute the response.
  • The request will be sent to a file on the server in response to the request.
  • As you can see from the URL, a parameter (a) was added (which contains the content of the input field).

The PHP File

Using the JavaScript as mentioned above, the JavaScript calls a PHP file on the server named “getPollVotes.php”:


<?php $resp = $_REQUEST['resp'];//get content of the text file $file = 'vote-result.txt'; $content = file($file);//add content to array $arr = explode(' — ', $content[0]); $yes = (int)$arr[0]; $no = (int)$arr[1];if ($resp == 0) { $yes += 1; } if ($resp == 1) { $no += 1; }//add votes into text file $add_vote = $yes . " — " . $no; $fp = fopen($file, 'w'); fputs($fp, $add_vote); fclose($fp); ?><h2>Results:</h2> <table> <tr> <td> <b>Yes!: </b> <img src='poll.gif' width='<?php echo (200 * round($yes / ($no + $yes), 2)); ?>' height='20'> <?php echo (200 * round($yes / ($no + $yes), 2)); ?>% </td> </tr> <tr> <td> <b>No!: </b> <img src='poll.gif' width='<?php echo (200 * round($no / ($no + $yes), 2)); ?>' height='20'> <?php echo (200 * round($no / ($no + $yes), 2)); ?>% </td> </tr> </table> <h3>Thankyou for your feedback!</h3>

Example Explanation

The code snippet is used to create a poll that allows users to vote and display the results in percentage. The script receives the user’s response through the $_REQUEST variable and updates the vote results stored in a text file accordingly. The results are then displayed using HTML.

  1. First, the script retrieves the content of a text file named vote-result.txt and stores it into an array $content. Then, the script explodes the first element of the array $content by the delimiter ‘ — ‘ and stores the result into the array $arr. The first value of the array $arr is assigned to the variable $yes, and the second value is assigned to the variable $no. These variables will be used to keep track of the vote count.
  2. Next, the script checks the user’s response stored in the variable $resp. If $resp is 0, it means the user has voted “Yes” and the variable $yes is incremented by 1. If $resp is 1, it means the user has voted “No” and the variable $no is incremented by 1.
  3. After the vote count is updated, the script concatenates the values of $yes and $no with the delimiter ‘ — ‘ and saves them back to the text file using the fopen, fputs, and fclose functions.
  4. Finally, the script displays the poll results in a table using HTML. The percentage of “Yes” and “No” votes are calculated by dividing the vote count by the total number of votes, then multiplying by 100 and rounding to two decimal places. The results are displayed using an image and the percentage is displayed next to it. A thank you message is also displayed at the end.

The Text File

There is a text file (vote-result.txt) which stores the vote data from the poll.

Here is how it is stored:

0 -- 0

There are two numbers in the text file. The first value represents the “Yes” votes, and the second value represents the “No” votes.

Note: Make sure that your web server is allowed to edit the text file on your website. Don’t give everyone access, only the PHP server (web server) should have access to the files.


In this article, we’ve shown you how to create a PHP AJAX poll that is both easy to implement and interactive for your users.

By following the steps outlined in this article, you can create a poll that engages your website visitors and provides valuable feedback for your business or organization.

With the power of PHP and AJAX, you can create dynamic web pages that keep your users engaged and coming back for more.

If you liked this article and found it informative regarding Php Ajax Poll, you can leave your feedback by reacting below.

We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *