PHP and AJAX Voting
- Previous Page AJAX RSS Reader
- Next Page PHP Array
AJAX Voting
In this AJAX example, we will demonstrate a voting program that can obtain results without reloading the web page.
So far, do you like PHP and AJAX?
This example includes four elements:
- HTML form
- JavaScript
- PHP page
- Result storing text file
HTML form
This is an HTML page. It includes a simple HTML form, and a link to a JavaScript file:
<html> <head> <script src="poll.js"></script> </head> <body> <div id="poll"> <h2>Do you like PHP and AJAX so far?</h2> <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>
Example Explanation - HTML Form
As you can see, the above HTML page contains a simple HTML form with a <div> element containing two radio buttons.
How the form works:
- An event is triggered when the user selects "yes" or "no"
- The getVote() function is executed when the event is triggered
- The form is surrounded by a <div> named "poll". When data is returned from the getVote() function, the returned data replaces the form.
Text file
The text file (poll_result.txt) stores the data from the voting program.
It looks something like this:
0||0
The first number represents the "Yes" vote, and the second number represents the "No" vote.
Note:Remember to allow only your web server to edit this text file. Do not give others access rights, except for the web server (PHP).
JavaScript
The JavaScript code is stored in "poll.js" and is linked to the HTML document:
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 || xmlHttp.readyState=="complete") { 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 }
Example Explanation:
The stateChanged() and GetXmlHttpObject functions are with PHP and AJAX Requests The examples in this section are the same.
getVote() function
This function is executed when the user selects "yes" or "no" in the HTML form.
- Define the url (filename) to be sent to the server
- Add parameters (vote) to the url, which contain the content of the input field
- Add a random number to prevent the server from using cached files
- Call the GetXmlHttpObject function to create an XMLHTTP object and inform the object to execute the stateChanged function when a change is triggered
- Open an XMLHTTP object using the given url
- Send an HTTP request to the server
PHP page
The server page called by JavaScript code is a simple PHP file named "poll_vote.php".
<?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>
Example Explanation:
The selected value is transmitted from JavaScript, and then the following will occur:
- Get the content of the "poll_result.txt" file
- Put the file content into a variable and increment the selected variable by 1
- Write the results to the "poll_result.txt" file
- Output graphical voting results
- Previous Page AJAX RSS Reader
- Next Page PHP Array