PHP - AJAX voting
AJAX Voting
In the following example, we will demonstrate a voting program through which the voting results are displayed without refreshing the web page. show.
PHP中文网(php.cn) 你喜欢 PHP 和 AJAX 吗?
When the user selects one of the above options, a function named "getVote()" will be executed. This function is triggered by the "onclick" event.
getVote() function will perform the following steps:
· Create an XMLHttpRequest object
· Create a function that is executed when the server response is ready
· Send a message to the server File send request on
· Please note the parameter (q) added to the end of the URL (containing the contents of the drop-down list)
PHP file
The server page called above through JavaScript is a PHP file named "poll_vote.php":
结果:
是: | % |
否: | % |
When the selected value is sent from JavaScript to the PHP file, What will happen:
1. Get the contents of the "poll_result.txt" file
2. Put the file contents into a variable and add 1 to the selected variable
3. Write the results to the "poll_result.txt" file
4. Output the graphical voting results
##Text file
The data from the voting program is stored in the text file (poll_result.txt). The data it stores is as follows:The first number represents The number of votes for "Yes", the second number represents the number of votes for "No".5||3
Note: Please remember to only allow your web server to edit this text file. Don't let anyone else gain access except the web server (PHP).