I have been developing an appointment system over the past few weeks using php, mysql and javascript. I've come across a problem when trying to insert data into a table. I am using javascript on a php page to send a GET request to a php file which generates a table from database records. I have added a 'Book Appointment' button at the end of each row which should let the user confirm their appointment if its available and book it. Ive wrote all the code to get all relevant data from the table and then insert into a new table. The problem is that it does not insert. If I did this in php i could do this the way how im doing it now, but it would not be dynamic. I do not get any errors, it just does not insert the data. Do I need to do a ajax request in the javascript? I know theres lots of available help online with these sorts of problems, but cant seem to find an answer for my particular problem.
Heres my code:
<body>
<div id="textDisplay"<h1>Doctor availability and appointments will be listed here...</h1></div>
</body>
<script>
function showAppointments(str, patient_id, patient_surname) {
if (str == "" || patient_id == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("textDisplay").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getAppointments.php?listOption="+str + "&patient_id="+patient_id + "&surname="+patient_surname, true);
xmlhttp.send();
}
}
}
This javascript function sends a get request to a php page with some data appended in the url so it can be picked up by the php page.
getAppointments.php then generates the table with relevant database records.
***** getAppointments.php: *****
include 'dbConnect.php';
$listOptionVal = intval($_GET['listOption']);
$patient_id = $_GET['patient_id'];
$surname = $_GET['surname'];
if ($listOptionVal == 0) {
$sql="SELECT * FROM patient_info, appointment_slots INNER JOIN doctors ON appointment_slots.doctor_id=doctors.doctor_id WHERE
appointment_time BETWEEN '09:00:00' AND '12:00:00' AND appointment_date = '2015-11-02' AND patient_id = $patient_id;";
$result = $conn->query($sql);
echo "<center><h2>Available Appointments</h2><center>";
echo "<center><table>
<tr>
<th>Patient ID</th>
<th>Patient Surname</th>
<th>Date</th>
<th>Times</th>
<th>Avaialable</th>
<th>Doctor</th>
<th>Book Appointment</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo '<input type="hidden" name="patient_id" value="' . $row['patient_id'] . '" />';
echo '<input type="hidden" name="surname" value="' . $row['surname'] . '" />';
echo '<input type="hidden" name="appointment_date" value="' . $row['appointment_date'] . '" />';
echo '<input type="hidden" name="appointment_time" value="' . $row['appointment_time'] . '" />';
echo '<input type="hidden" name="appointment_avaliability" value="' . $row['appointment_avaliability'] . '" />';
echo "<form action='confirmed-appointments.php' method='post' enctype='multipart/form-data'>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['appointment_avaliability'] . "</td>";
if ($row['appointment_avaliability'] == 'Y') {
echo "<td style='color: #ACFB62;'>".$row['doctor_forename']." </td>";
} else if ($row['appointment_avaliability'] == 'N') {
echo "<td style='color: #FF6666;'>".$row['doctor_forename']." </td>";
}
echo "<td>
<input type='submit' name='bookAppointment' value='Book Appointment'>
</td>";
echo "</tr>";
}
}
echo "</table></center>";
echo "</form>";
This will then generate a table with appointment availability times. There is a button appended to each row allowing the user to book that appointment. This is where I want to send the data to the database and then loop through the results and show on a new page.
**** confirmed appointments.php - should be looping through rows of inserted data ****
<?php
include 'dbConnect.php';
if (isset($_POST['bookAppointment'])) {
var_dump($_POST);
$patient_id = $_POST['patient_id'];
$surname = $_POST['surname'];
$forename = $_POST['appointment_date'];
$dob = $_POST['appointment_time'];
$doctor = $_POST['doctor_forename'];
$sql = "INSERT INTO confirmed_appointments SET patient_id='" . $patient_id . "', surname='" . $surname . "', appointment_date='" . $appointment_date . "', appointment_time='" . $appointment_time . "', doctor='" . $doctor . "'";
if ($conn->query($sql) === TRUE) {
echo "Record added successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
}
$sql = "SELECT * from confirmed_appointments;";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<table border = 1>";
echo "<tr>";
echo "<td>" . $row['patient_id'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['appointment_date'] . "</td>";
echo "<td>" . $row['appointment_time'] . "</td>";
echo "<td>" . $row['doctor'] . "</td>";
echo "</tr>";
echo "</table>";
}
$conn->close();
?>
The data does not get inserted so therefore does not get displayed on the confirmed-appointments.php page. I hope i've been clear enough and I hope my question does not get closed!! Thanks for your time