I’m working on a simple bidding system and need help with real-time updates. Right now when someone places a bid, only their page refreshes to show the new amount. Other people viewing the same page don’t see the updated bid unless they manually refresh.
I want all users to automatically see new bids without having to refresh their browsers. I’ve heard about polling and AJAX but I’m not sure which approach works best.
Here’s my current setup for displaying items:
<div class="item-container">
<h2 class="item-header">Kitchen Items</h2>
<?php
$connection = mysqli_connect("host","user","pass","database");
if (mysqli_connect_errno()) {
echo "Database connection failed: " . mysqli_connect_error();
}
$query = mysqli_query($connection,"SELECT * FROM bidding WHERE type = 'Kitchen' ORDER BY item_id ASC");
while($data = mysqli_fetch_array($query)) {
echo "<form class='bid-form' id='item" . $data['item_id'] . "'>
<input type='hidden' name='item_id' value='" . $data['item_id'] . "' />
<div class='item-block'>
<div class='item-title'>" . $data['product_name'] . "</div>";
echo "<img class='product-img' src='" . $data['photo_url'] . "' />";
echo "<div class='current-price'>Latest Bid: $<span class='price-display' id='price_" . $data['item_id'] . "'>" . $data['top_bid'] . "</span></div>";
echo "<div class='bidder-info'>Enter Name: <input type='text' class='name-field' name='user_name' /></div>";
echo "<div class='bid-info'>Enter Amount: <input type='text' class='amount-field' name='bid_amount' /></div>";
echo "<div class='submit-area'><input type='submit' name='submit_bid' value='Submit Bid' /></div>";
echo "</div></form>";
}
mysqli_close($connection);
?>
My JavaScript handles the bid submission:
$(document).ready(function(){
$('.bid-form').submit(function(){
var itemId = $(this).find('input[name="item_id"]').val();
var userName = $(this).find('input[name="user_name"]').val();
var bidAmount = $(this).find('input[name="bid_amount"]').val();
var currentPrice = $('#price_' + itemId).text();
var productName = $(this).find('.item-title').text();
if (userName == '') {
alert("Please enter your name");
return false;
}
if (bidAmount > currentPrice) {
alert("Bid accepted");
} else {
alert("Bid too low");
return false;
}
$.ajax({
type: "POST",
url: "process-bid.php",
data: {user_name: userName, bid_amount: bidAmount, item_id: itemId, product_name: productName},
success: function(response){
location.reload();
}
});
return false;
});
});
The PHP file that updates the database:
<?php
$connection = mysqli_connect("host","user","pass","bidding_db");
if (mysqli_connect_errno()) {
echo "Connection error: " . mysqli_connect_error();
}
$user_name = $_POST['user_name'];
$bid_amount = $_POST['bid_amount'];
$item_id = $_POST['item_id'];
$product_name = $_POST['product_name'];
$update_query = "UPDATE bidding SET top_bid = '$bid_amount', winner = '$user_name' WHERE item_id = '$item_id'";
$log_query = "INSERT INTO bid_history (product_name, amount, bidder_name) VALUES ('$product_name','$bid_amount','$user_name')";
mysqli_query($connection, $update_query);
mysqli_query($connection, $log_query);
mysqli_close($connection);
?>
I need a way to push these database changes to all users viewing the page. Any suggestions on how to implement this would be great. Thanks for any help you can provide.