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

  1. User selects an option and clicks Vote.

  2. submitVote() sends the selected option to vote.php using AJAX POST.

  3. PHP updates the votes in MySQL and returns updated results.

  4. 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.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *