PHP Instance-AJAX poll

Source: Internet
Author: User

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:

    1. Get the contents of the "Poll_result.txt" file
    2. Put the contents of the file into a variable and add 1 to the selected variable
    3. Write the results to the "poll_result.txt" file
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.