138 lines
7.6 KiB
PHP
138 lines
7.6 KiB
PHP
<?php
|
|
|
|
|
|
require_once 'db/config.php';
|
|
|
|
// Fetch all contracts for the dropdown
|
|
$pdo = db();
|
|
$contracts_stmt = $pdo->query('SELECT id, contract_title, customer_name FROM contracts ORDER BY customer_name, contract_title');
|
|
$contracts = $contracts_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$success_message = '';
|
|
$error_message = '';
|
|
$form_data = array_fill_keys(['name', 'phone', 'email', 'address', 'job_description', 'preferred_date', 'description', 'contract_id'], '');
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
// Sanitize and retrieve form data
|
|
$name = trim($_POST['name'] ?? '');
|
|
$phone = trim($_POST['phone'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$address = trim($_POST['address'] ?? '');
|
|
$job_description = trim($_POST['job_description'] ?? '');
|
|
$preferred_date = trim($_POST['preferred_date'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$contract_id = trim($_POST['contract_id'] ?? '');
|
|
|
|
// Store submitted data to re-populate the form on error
|
|
$form_data = compact('name', 'phone', 'email', 'address', 'job_description', 'preferred_date', 'description', 'contract_id');
|
|
|
|
// Server-side validation
|
|
if (empty($name) || empty($phone) || empty($address) || empty($job_description)) {
|
|
$error_message = 'Please fill in all required fields: Name, Phone, Address, and Service Type.';
|
|
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email)) {
|
|
$error_message = 'Please provide a valid email address.';
|
|
} else {
|
|
|
|
try {
|
|
$pdo = db();
|
|
$sql = "INSERT INTO service_requests (name, phone, email, address, job_description, preferred_date, description, contract_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
// Handle empty date and contract
|
|
$date_to_insert = !empty($preferred_date) ? $preferred_date : null;
|
|
$contract_to_insert = !empty($contract_id) ? $contract_id : null;
|
|
|
|
$stmt->execute([$name, $phone, $email, $address, $job_description, $date_to_insert, $description, $contract_to_insert]);
|
|
|
|
$success_message = "Thank you! Your service request has been submitted successfully. We will contact you shortly.";
|
|
// Clear form data on success
|
|
$form_data = array_fill_keys(array_keys($form_data), '');
|
|
|
|
} catch (PDOException $e) {
|
|
// Debugging: show exact error
|
|
//var_dump($e->getMessage());
|
|
error_log("Service Request Error: " . $e->getMessage());
|
|
$error_message = 'Sorry, there was an error submitting your request. Please try again later.';
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h2 class="card-title text-center mb-4">Submit a Service Request</h2>
|
|
|
|
<?php if ($success_message): ?>
|
|
<div class="alert alert-success">
|
|
<?php echo $success_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo $error_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="request-service.php" method="POST" id="service-form" novalidate>
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Full Name <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($form_data['name']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="phone" class="form-label">Phone Number <span class="text-danger">*</span></label>
|
|
<input type="tel" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($form_data['phone']); ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email Address</label>
|
|
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($form_data['email']); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="address" class="form-label">Full Address <span class="text-danger">*</span></label>
|
|
<textarea class="form-control" id="address" name="address" rows="3" required><?php echo htmlspecialchars($form_data['address']); ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="job_description" class="form-label">Service Required <span class="text-danger">*</span></label>
|
|
<select class="form-select" id="job_description" name="job_description" required>
|
|
<option value="" disabled <?php echo empty($form_data['job_description']) ? 'selected' : ''; ?>>Choose...</option>
|
|
<option value="Installation" <?php echo ($form_data['job_description'] == 'Installation') ? 'selected' : ''; ?>>New Installation</option>
|
|
<option value="Repair" <?php echo ($form_data['job_description'] == 'Repair') ? 'selected' : ''; ?>>Repair & Troubleshooting</option>
|
|
<option value="AMC Service" <?php echo ($form_data['job_description'] == 'AMC Service') ? 'selected' : ''; ?>>AMC Service</option>
|
|
<option value="Filter Change" <?php echo ($form_data['job_description'] == 'Filter Change') ? 'selected' : ''; ?>>Filter Change</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="contract_id" class="form-label">Associated Contract (if any)</label>
|
|
<select class="form-select" id="contract_id" name="contract_id">
|
|
<option value="" selected>None</option>
|
|
<?php foreach ($contracts as $contract): ?>
|
|
<option value="<?php echo $contract['id']; ?>"
|
|
<?php echo ($form_data['contract_id'] == $contract['id']) ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($contract['customer_name'] . ' - ' . $contract['contract_title']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="preferred_date" class="form-label">Preferred Service Date</label>
|
|
<input type="date" class="form-control" id="preferred_date" name="preferred_date" value="<?php echo htmlspecialchars($form_data['preferred_date']); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Problem Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($form_data['description']); ?></textarea>
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary btn-lg">Submit Request</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|