-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduleProcedure.php
More file actions
270 lines (229 loc) · 10.3 KB
/
scheduleProcedure.php
File metadata and controls
270 lines (229 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("connection.php");
session_start();
$error = null;
if (isset($_POST["submit"])) {
$procedureDate = $_POST['procedureDate'];
$procedureType = $_POST['procedureType'];
$appointmentID = $_POST['appointmentID'];
$appointmentExists = checkIfAppointmentExists($appointmentID, $con);
$procedureExists = checkIfProcedureExists($appointmentID, $con);
$ProcedureID = generateRan($con);
$DoctorName = getDoctorName($appointmentID, $con);
$patientID = getPatientID($appointmentID, $con);
// Check if the appointment exists
if (!$appointmentExists) {
// Patient does not exist, send an error message
echo '<script>
alert("Appointment not found. Please scheudle a pre-procedure appointment.");
window.location.href = "scheduleAppt.php"
</script>';
exit();
}
if ($procedureExists){
// Patient does not exist, send an error message
echo '<script>
alert("Procedure already exists. Delete Procedure to continue, or enter new ID>");
window.location.href = "scheduleProcedure.php"
</script>';
exit();
}
echo '<script>
const confirmation = confirm("Are you sure that you want to scehedule an procedure?");
if (!confirmation) {
alert("Update Cancelled");
} else {
const xhr = new XMLHttpRequest();
xhr.open("POST", "scheduleProcedure.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Add other form data to the request if needed
const formData = new FormData(document.getElementById("scheduleProcedure"));
xhr.send(formData);
// Handle the response from the server-side PHP file
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
alert(xhr.responseText); // Display the response from PHP
}
};
}
</script>';
try {
// Start a transaction
$con->begin_transaction();
// Insert into Procedures table
$query = "INSERT INTO Procedures (ProcedureID, ProcedureDate, ProcedureType, DoctorName, PatientID, PreAppointmentID) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $con->prepare($query);
$stmt->bind_param("isssii", $ProcedureID, $procedureDate, $procedureType, $DoctorName, $patientID, $appointmentID);
$stmt->execute();
if ($stmt->error) {
throw new Exception("Query 1 failed: " . $stmt->error);
}
$stmt->close();
// Update patientAppts table
$query = "UPDATE patientAppts SET procedureDate = ?, procedureID = ?, procedureType = ? WHERE appointmentID = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("sisi", $procedureDate, $ProcedureID, $procedureType, $appointmentID);
$stmt->execute();
if ($stmt->error) {
throw new Exception("Query 2 failed: " . $stmt->error);
}
$stmt->close();
// Commit the transaction if everything is successful
$con->commit();
} catch (Exception $e) {
// An error occurred, rollback the transaction
$con->rollback();
echo "Transaction failed: " . $e->getMessage();
}
echo '<script>
alert("Procedure Appointment Scheduled.");
//window.location.href = "scheduleProcedure.php";
</script>';
}
// Function to check if the patient exists
function checkIfAppointmentExists($appointmentID, $con) {
$query = "SELECT appointmentID FROM patientAppts WHERE appointmentID = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $appointmentID);
$stmt->execute();
$stmt->store_result();
$rowCount = $stmt->num_rows;
$stmt->close();
return $rowCount > 0;
}
function checkIfProcedureExists($appointmentID, $con) {
$query = "SELECT procedureID FROM patientAppts WHERE appointmentID = ? AND procedureID IS NOT NULL";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $appointmentID);
$stmt->execute();
$stmt->store_result();
$rowCount = $stmt->num_rows;
$stmt->close();
return $rowCount > 0;
}
function generateRan($con) {
$randomNumber = rand(100000, 999999);
$query = "SELECT ProcedureID FROM Procedures WHERE ProcedureID = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $randomNumber);
$stmt->execute();
$stmt->store_result();
$rowCount = $stmt->num_rows;
$stmt->close();
if ($rowCount > 0) {
// Recursively call generateRan until a unique ID is found
return generateRan($con);
} else {
return $randomNumber;
}
}
function getDoctorName($appointmentID, $con) {
$DoctorName = null;
if ($stmt = $con->prepare("SELECT physicianName FROM patientAppts WHERE appointmentID = ?")) {
$stmt->bind_param("i", $appointmentID);
$stmt->execute();
$stmt->bind_result($DoctorName);
$stmt->fetch();
$stmt->close();
return $DoctorName;
} else {
// Output the MySQL error for debugging
echo "Error in SQL query: " . $con->error;
return null;
}
}
function getPatientID($appointmentID, $con) {
$PatientID = null;
if ($stmt = $con->prepare("SELECT PatientID FROM patientAppts WHERE appointmentID = ?")) {
$stmt->bind_param("i", $appointmentID);
$stmt->execute();
$stmt->bind_result($PatientID);
$stmt->fetch();
$stmt->close();
return $PatientID;
} else {
// Output the MySQL error for debugging
echo "Error in SQL query: " . $con->error;
return null;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Schedule Patient Appointment</title>
<link rel="stylesheet" href="newStyle.css">
</head>
<body>
<div class = "navbar"></div>
<nav>
<a href="searchReceptionist.php">Search Receptionist</a>
<a href="updateInfo.php">Update Patient Information</a>
<a href="verifyPatient.php">Schedule Patient Appoin`tment</a>
<a href="cancelAppt.php">Cancel Patient Appointment</a>
<a href="scheduleProcedure.php">Schedule Patient Procedure</a>
<a href="cancelProcedure.php">Cancel A Patient Procedure</a>
<a href="createNewPatient.php">Create A New Patient</a>
<a href="databasemain.html">Access Database Main</a>
</nav>
</div>
<form id="scheduleProcedure" action="scheduleProcedure.php" method="POST" onsubmit="return validateForm()">
<div class="container">
<div class="header">
<h2>Schedule New Procedure </h2>
</div>
<div class="UserInput">
<div class="enter1">
<p>Input Procedure Date</p>
<input type="text" name="procedureDate" id = 'procedureDate' placeholder="Input Procedure Date (2023-02-23) Format" style="color: black">
<p><strong>REQUIRED</strong></p>
</div>
<div class="enter2">
<p>Input Procedure Type</p>
<input type="text" name="procedureType" id = 'procedureType' placeholder="Insert Procedure Type" style="color: black">
<p><strong>REQUIRED</strong></p>
</div>
<div class="enter3">
<p>Input Pre Appointment ID:</p>
<input type="text" name="appointmentID" id = "appointmentID" placeholder="Insert Appointment ID" style="color: black">
<p><strong>REQUIRED</strong></p>
</div>
<div class="footer">
<button type="submit" class="btn" name = "submit">Submit</button>
<button type="reset" class="btn">Reset</button>
<button id = "homeButton" class = "btn" onclick = "redirect()">Home Button</button>
</div>
</div>
</form>
<script>
function redirect() {
// Redirect to the Doctors Receptionist Database page
window.location.href = "main.html";
exit();
}
function validateForm() {
// Validate date on the client side
const dateInput = document.getElementsByName('procedureDate')[0].value;
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
// Display error message if validation fails
if (!dateRegex.test(dateInput)) {
alert("Please enter a valid date in the format YYYY-MM-DD");
return false; // Prevent form submission
}
// Validate date on the client side
const IDInput = document.getElementsByName('appointmentID')[0].value;
const IDRegex = /^\d{3}$/;
// Display error message if validation fails
if (!IDRegex.test(IDInput)) {
alert("Please enter a valid ID in the format of 3 digit integer value");
return false; // Prevent form submission
} else {
return true; // Allow form submission
}
}
</script>
</body>
</html>