I have a table residing on a MySql data base called Bookings.
The Bookings table consists of a composite primary key based upon the following 2 fields: BookingDate & Booking Slot.
On my website, I have built a bookings page which will consist of a a simple HTML Form, which has a Date Field and a Radio-Button group which has 3 different time slots (eg: 9am, 2pm and 4pm). Firstly, the User will select a Date they would wish to place a booking on and then click a submit button called "Check Availablity". Once this button is clicked, Ideally, I would like a script to check an see what free slots are available for that Date and then disable the radio buttons related the slots which are already booked.
Here is a simple scenario to help explain the functionality i am trying to implement :
The User comes to the Bookings page, and selects the Date 14/03/2011. Then clicks the "Check Availablity" button. In this scenario, the 9am slot is already booked for this date (eg: this booking already exists on the MySql database table), so the 9am radio button on the page is then disabled, not allowing the user to try an attempt to book this timeslot on this date.
Below is the a simplified version of the php script which I wrote to implement the this functionality.
<?php
function checkSlot()
{
// Get data
$test_date = $_GET["bookingDate"];
// Database connection
$conn = mysqli_connect("localhost","root","","wordpress");
if(!$conn) {
die('Problem in database connection: ' . mysql_error());
}
// Attempt to find Duplicate Primary Keys
$query = "SELECT Count(*) AS 'total' FROM Bookings WHERE booking_date ='{$booking_date}' AND booking_slot = '9am'";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
$num = $result->fetch_assoc();
If ($num['total'] == 0)
{
//echo "<script> document.getElementById('9amTimeSlotRadioButton').disabled = false; </script>";
}
else if($num['total'] > 0)
{
//echo "<script> document.getElementById('9amTimeSlotRadioButton').disabled = true; </script>";
}
}
?>
My problem is that I cant get the Javascript line within the if statement to execute and disable/enable the related 9am radio button.
Upon research into this, I have recently found out that it is not possible to do this with just Php and JavaScript, as php runs serverside and javascript runs client-side and Ajax would be needed here.
How to implement this functionality by using Ajax?
I have zero experience in the language so I really dont know where to start.