Creating an AJAX poll in PHP involves building a simple polling system that allows users to vote for a certain option, and then displaying the updated poll results without refreshing the entire page. Here's a basic example of how you can implement an AJAX poll using HTML, JavaScript, and PHP:
1. HTML (index.html):
Create an HTML page that displays the poll options and updates the results using AJAX.
html
<!DOCTYPE html>
<html>
<head>
<title>AJAX Poll Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function vote(option) {
$.ajax({
type: "POST",
url: "vote.php",
data: { option: option },
success: function(response) {
$("#poll-results").html(response);
}
});
}
</script>
</head>
<body>
<h2>Poll</h2>
<div id="poll-options">
<button onclick="vote('option1')">Option 1</button>
<button onclick="vote('option2')">Option 2</button>
<!-- Add more options as needed -->
</div>
<div id="poll-results">
<!-- Poll results will be displayed here -->
</div>
</body>
</html>
2. PHP (vote.php):
Create a PHP script that handles the voting process and returns updated poll results.
php
<?php
// Assuming you have a database connection
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['option'])) {
$option = $_POST['option'];
// Update the vote count in the database
$sql = "UPDATE poll_options SET votes = votes + 1 WHERE option_name = '$option'";
$conn->query($sql);
}
// Retrieve poll results from the database
$results = array();
$sql = "SELECT option_name, votes FROM poll_options";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$results[$row['option_name']] = $row['votes'];
}
// Display poll results
foreach ($results as $option => $votes) {
echo "$option: $votes votes<br>";
}
$conn->close();
?>
3. Database Table (poll_options):
Create a database table named "poll_options" to store the poll options and their vote counts.
sql
CREATE TABLE poll_options (
id INT AUTO_INCREMENT PRIMARY KEY,
option_name VARCHAR(50) NOT NULL,
votes INT NOT NULL DEFAULT 0
);
INSERT INTO poll_options (option_name) VALUES ('option1'), ('option2');
In this example, users can click on the poll options, and the `vote()` JavaScript function sends an AJAX POST request to the `vote.php` script. The PHP script updates the vote count in the database and retrieves the updated poll results, which are then displayed on the page.
Keep in mind that this is a basic example. In a real-world scenario, you would likely implement more security measures, error handling, and possibly user authentication to prevent abuse of the polling system.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc