AJAX Poll

The following example will demonstrate a poll where the result is shown without reloading.

Do you like PHP and AJAX so far?

Yes: 
No: 

Example Explained – The HTML Page

When a user chooses an option above, a function called “getVote()” is executed. The function is triggered by the “onclick” event:

 

Do you like PHP and AJAX so far?

 

Yes:
No:

 

 


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

The page on the server called by the JavaScript above is a PHP file called “poll_vote.php”:

Result:

 

 

Yes: %
No: %