PHP Example - AJAX Poll

PHP Example - AJAX Voting

AJAX Voting

In the following example, we will demonstrate a voting program, through which the voting results are not carried out on the web page Displayed when refreshed.

QQ图片20161010104701.png

Explanation of examples - HTML page

When the user selects one of the above options, a function named "getVote()" will be executed. This function is triggered by the "onclick" event.

poll.html file code is as follows:

   php中文网(php.cn)  

你喜欢 PHP 和 AJAX 吗?

是:
否:

getVote() function will perform the following steps:

Create XMLHttpRequest object

Created when the server response is ready The executed function

Sends a request to the file on the server

Please note the parameter (q) added to the end of the URL (contains the contents of the drop-down list)

PHP file

The server page called by JavaScript above is a PHP file named "poll_vote.php":

 

结果:

是: %
否: %

When the selected value is sent from JavaScript to the PHP file, what will happen:

Get the contents of the "poll_result.txt" file

Put the file contents into the variable and add 1 to the selected variable

Write the result to the "poll_result.txt" file

Output graphical voting results

Text file

The text file (poll_result.txt) stores the data from the voting program.

The data it stores is as follows:

3||4

The first number represents the number of votes for "Yes", and the second number represents The number of "No" votes.

Note: Please remember to allow only your web server to edit this text file. Don't let anyone else gain access except the web server (PHP).


Continuing Learning
||
php中文网(php.cn)

你喜欢 PHP 和 AJAX 吗?

是:
否:
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!