PHP Instance-AJAX Polls AJAX Polls
In the following example, we will demonstrate a voting program through which the poll results are displayed without refreshing the page.
Do you like PHP and AJAX? Example explanation-HTML page
When the user selects an option above, a function named "Getvote ()" is executed. This function is triggered by an "onclick" event.
The poll.html file code is as follows:
<HTML><Head><MetaCharSet= "Utf-8"><title>Beginner's Tutorial (runoob.com)</title><Script>functionGetvote (int) { if(window. XMLHttpRequest) {//ie7+, Firefox, Chrome, Opera, Safari code executionXMLHTTP=NewXMLHttpRequest (); } Else { //IE6, IE5 code executionXMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); } Xmlhttp.onreadystatechange=function() { if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) {document.getElementById ("Poll"). InnerHTML=Xmlhttp.responsetext; }} xmlhttp.open ("GET","poll_vote.php?vote="+int,true); Xmlhttp.send ();}</Script></Head><Body><DivID= "Poll"><H3>Do you like PHP and AJAX?</H3><form>are:<inputtype= "Radio"name= "vote"value= "0"onclick= "Getvote (this.value)"><BR>No:<inputtype= "Radio"name= "vote"value= "1"onclick= "Getvote (this.value)"></form></Div></Body></HTML>
The Getvote () function performs the following steps:
- Create a XMLHttpRequest Object
- Create a function that executes when the server responds ready
- Send a request to a file on the server
- Note the parameter (q) added to the end of the URL (contains the contents of the drop-down list)
PHP file
The above server page that is called by JavaScript is a php file named "poll_vote.php":
<?PHP$vote=Htmlspecialchars($_request[' Vote ']);//get the data stored in the file$filename= "Poll_result.txt";$content=file($filename);//splitting data into arrays$array=Explode("||",$content[0]);$yes=$array[0];$no=$array[1];if($vote= = 0){ $yes=$yes+ 1;}if($vote= = 1){ $no=$no+ 1;}//Insert poll Data$insertvote=$yes."||".$no;$fp=fopen($filename, "W");fputs($fp,$insertvote);fclose($fp);?>Display:inline-block; background-color:green; width:<?php Echo (100*round ($yes/($no+$yes), 2);?>px; height:20px;"></span> <?phpEcho(100*round($yes/($no+$yes), 2); >% </td> </tr> <tr> <td> no:</td> <td> <span style= "Display:inline-block; background-color:red; width:<?php Echo (100*round ($no/($no+$yes), 2);?>px; height:20px;"></span> <?phpEcho(100*round($no/($no+$yes), 2); >% </td> </tr></table>
When the selected value is sent from JavaScript to a PHP file, it occurs:
- Get the contents of the "Poll_result.txt" file
- Put the contents of the file into a variable and add 1 to the selected variable
- Write the results to the "poll_result.txt" file
- Output a graphical poll result
Text file
The data from the voting program is stored in a text file (poll_result.txt).
The data it stores is as follows:
3| | 4
The first number indicates the number of votes for "Yes", and the second number indicates the number of votes for "No".
Note: Please remember to only allow your Web server to edit the text file. do not let other people gain access, except Web server (PHP).
PHP Instance-AJAX poll