-- Learning not just technology, but dreams too!
Home HTML JAVASCRIPT CSS VUE REACT PYTHON3 JAVA C C++ C# AI GO SQL LINUX VS CODE BOOTSTRAP GIT Local Bookmarks
PHP Tutorial
PHP Tutorial
PHP Introduction
PHP Installation
PHP Syntax
PHP Variables
PHP echo/print
PHP EOF (heredoc)
PHP Data Types
PHP Type Comparison
PHP Constants
PHP Strings
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP Array Sorting
PHP Superglobals
PHP While Loop
PHP For Loop
PHP Functions
PHP Magic Constants
PHP Namespaces
PHP Object-Oriented Programming
PHP Quiz
PHP Forms
PHP Forms
PHP Form Validation
PHP Forms - Required Fields
PHP Forms - Email and URL Validation
Complete PHP Form Example
PHP $_GET Variable
PHP $_POST Variable
Advanced PHP Tutorials
PHP Multidimensional Arrays
PHP Dates
PHP Includes
PHP Files
PHP File Uploads
PHP Cookies
PHP Sessions
PHP E-mail
PHP Secure E-mail
PHP Errors
PHP Exceptions
PHP Filters
Advanced PHP Filters
PHP JSON
New Features in PHP 7
New Features in PHP 7
PHP Databases
Introduction to PHP MySQL
Connecting to PHP MySQL
Creating a Database with PHP MySQL
Creating Tables with PHP MySQL
Inserting Data with PHP MySQL
Inserting Multiple Records with PHP MySQL
PHP Prepared Statements
Reading Data with PHP MySQL
PHP MySQL WHERE Clause
PHP MySQL ORDER BY
PHP MySQL UPDATE
PHP MySQL DELETE
PHP ODBC
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP and AJAX
Introduction to AJAX
AJAX in PHP
AJAX Databases
AJAX XML
AJAX Real-Time Search
AJAX RSS Reader
AJAX Voting
PHP Reference Manual
PHP Array
PHP Calendar
PHP cURL
PHP Date
PHP Directory
PHP Error
PHP Filesystem
PHP Filter
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Math
PHP Misc
PHP MySQLi
PHP PDO
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Time Zones
PHP Image Processing
PHP RESTful
PHP PCRE
Available PHP Functions
PHP Composer
PHP Example - AJAX RSS Reader
PHP 5 Array Functions
In-Depth Exploration
Data Management
Scripting Languages
script
Programming Languages
Development Tools
Web Services
Computer Hardware
Computers
Web Design and Development
PHP Example - AJAX Voting
AJAX Voting
In the following example, we will demonstrate a voting program where the voting results are displayed without refreshing the webpage.
Do you like PHP and AJAX?
Yes:
No:
Example Explanation - HTML Page
When the user selects one of the options above, the function named "getVote()" is executed. This function is triggered by the "onclick" event.
The code for the poll.html file is as follows:
function getVote(int) {
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari execute this code
xmlhttp = new XMLHttpRequest();
} else {
// IE6, IE5 execute this code
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("poll").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "poll_vote.php?vote=" + int, true);
xmlhttp.send();
}
Do you like PHP and AJAX?
Yes:
No:
The getVote() function performs the following steps:
Create an XMLHttpRequest object
Create a function that runs when the server response is ready
Send a request to a file on the server
Note the parameter added to the end of the URL (q), which contains the content of the dropdown list
PHP File
The server page called by JavaScript above is a PHP file named "poll_vote.php":
Results:
| Yes: |
<span style="display: inline-block; background-color: green;
width: px;
height: 20px;">
%
|
| No: |
<span style="display: inline-block; background-color: red;
width: px;
height: 20px;">
%
|
When the selected value is sent from JavaScript to the PHP file, the following happens:
Retrieve the contents of the "poll_result.txt" file
Store the file contents in a variable and increment the selected option by 1
Write the result back to the "poll_result.txt" file
Output the graphical voting results
Text File
The text file ("poll_result.txt") stores data from the voting program.
It stores data like this:
3||4
The first number represents the number of "Yes" votes, and the second number represents the number of "No" votes.
Note: Remember to allow only your web server to edit this text file. Do not grant access to anyone else except the web server (PHP).
PHP Example - AJAX RSS Reader
PHP 5 Array Functions
Byte Ark Coding Plan
Supports mainstream large models such as Doubao, GLM, DeepSeek, Kimi, MiniMax, etc., directly provided by official sourcesβstable and reliable.
Β₯9.9
/ month
Activate Now
iFLYTEK Starry Sky Coding Plan
Includes free model call credits for DeepSeek, GLM, Kimi, MiniMaxβa one-stop platform for experience and deployment.
Β₯3.9
/ month
Activate Now
2 Notes
Write Note
Β Β Β Daughter of the Study God
Β Β dff***j@qq.com
128
This is very insecure! Attackers can break it with a short piece of code!
// Infinite loop script
var Vote = 0; // Your vote.
setInterval(function(){
getVote(Vote);
}, 2000);
How can it be made more secure? You can use cookies to record votes, so attackers would also have to clear the cookies.
if(empty($_COOKIE)) {
setcookie("voted","yes!",ime()+60*60*24*365);
} else {
die("You've already voted!");
}
Daughter of the Study God
Β Β Β Daughter of the Study God
Β Β dff***j@qq.com
8 years ago (2018-05-08)
Β Β Β Now Internet
Β Β che***nzho@aliyun.com
149
I strongly disagree with @Daughter of the Study God: βHow can it be made more secure? Use cookies to record votes, so attackers would have to clear the cookies.β
Thatβs actually not ideal either. As long as I never carry cookies, I can keep submitting votes to cheat, like this:
<?php
// Based on browser packet capture, the voting URL is as follows:
$api_url = "
for($i=0; $i<1000; $i++){
// Loop 1000 times using file_get_contents to access the voting URL and cheat
file_get_contents($api_url);
}
To prevent cheating, you still need to limit IP addresses and require cookies.
Alternatively, you could require users to log in before voting, perhaps by integrating WeChat login so users must log in via WeChat to cast their votes.
Now Internet
Β Β Β Now Internet
Β Β che***nzho@aliyun.com
6 years ago (2020-02-05)
Category Navigation
Python / Data Science
AI / Intelligent Development
Frontend Development
Backend Development
Databases
Mobile Development
DevOps / Engineering
Programming Languages
Computer Fundamentals
XML / Web Services
.NET
Website Building
Advertisement
PHP Tutorial
PHP Tutorial
PHP Introduction
PHP Installation
PHP Syntax
PHP Variables
PHP echo/print
PHP EOF (heredoc)
PHP Data Types
PHP Type Comparison
PHP Constants
PHP Strings
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP Array Sorting
PHP Superglobals
PHP While Loops
PHP For Loops
PHP Functions
PHP Magic Constants
PHP Namespaces
PHP Object-Oriented Programming
PHP Quiz
PHP Forms
PHP Forms
PHP Form Validation
PHP Forms - Required Fields
PHP Forms - Email and URL Validation
Complete PHP Form Example
PHP $_GET Variable
PHP $_POST Variable
Advanced PHP Tutorials
PHP Multidimensional Arrays
PHP Dates
PHP Includes
PHP Files
PHP File Uploads
PHP Cookies
PHP Sessions
PHP E-mail
PHP Secure E-mail
PHP Errors
PHP Exceptions
PHP Filters
Advanced PHP Filters
PHP JSON
New Features in PHP 7
New Features in PHP 7
PHP Databases
Introduction to PHP MySQL
Connecting to PHP MySQL
Creating Databases with PHP MySQL
Creating Tables with PHP MySQL
Inserting Data with PHP MySQL
Inserting Multiple Records with PHP MySQL
PHP Prepared Statements
Reading Data with PHP MySQL
PHP MySQL WHERE Clause
PHP MySQL ORDER BY
PHP MySQL UPDATE
PHP MySQL DELETE
PHP ODBC
PHP XML
XML Expat Parser
XML DOM
XML SimpleXML
PHP and AJAX
Introduction to AJAX
AJAX in PHP
AJAX Databases
AJAX XML
AJAX Real-Time Search
AJAX RSS Reader
AJAX Voting
PHP Reference Manual
PHP Array
PHP Calendar
PHP cURL
PHP Date
PHP Directory
PHP Errors
PHP Filesystem
PHP Filters
PHP FTP
PHP HTTP
PHP Libxml
PHP Mail
PHP Math
PHP Misc
PHP MySQLi
PHP PDO
PHP SimpleXML
PHP String
PHP XML