PHP AJAX Poll Example
📊 PHP AJAX Poll Example
A poll lets users vote on a question, and using AJAX and PHP, we can update the poll results dynamically without reloading the page.
1️⃣ Database Setup
Table: poll
| id | option | votes |
|---|---|---|
| 1 | Option A | 0 |
| 2 | Option B | 0 |
| 3 | Option C | 0 |
2️⃣ PHP Script to Display Poll (poll.php)
3️⃣ PHP Script to Process Vote (vote.php)
4️⃣ HTML + JavaScript (index.html)
5️⃣ How It Works
-
User selects an option and clicks Vote.
-
submitVote()sends the selected option tovote.phpusing AJAX POST. -
PHP updates the votes in MySQL and returns updated results.
-
JavaScript updates the poll results dynamically in
div#pollResult.
6️⃣ Key Points
-
AJAX allows real-time update of poll results without reloading.
-
Use POST method to send user votes securely.
-
Always validate and sanitize inputs in PHP.
-
Can be enhanced with charts or percentages for better UX.
-
Suitable for website polls, surveys, or voting apps.
