false, 'message' => 'Invalid CSRF token']);
exit;
}
// Validate required fields
$required_fields = ['program', 'name', 'father_name', 'address', 'class', 'payment_method', 'place'];
foreach ($required_fields as $field) {
if (empty($_POST[$field])) {
logError("Missing required field: $field");
echo json_encode(['success' => false, 'message' => 'Please fill all required fields']);
exit;
}
}
$program = sanitizeInput($_POST['program']);
$name = sanitizeInput($_POST['name']);
$father_name = sanitizeInput($_POST['father_name']);
$address = sanitizeInput($_POST['address']);
$class = sanitizeInput($_POST['class']);
$payment_method = sanitizeInput($_POST['payment_method']);
$place = sanitizeInput($_POST['place']);
$email = !empty($_POST['email']) ? sanitizeInput($_POST['email']) : null;
// Amount 0 for free, else parse
$amount = $payment_method === 'free' ? 0.00 : floatval($_POST['amount'] ?? 0);
// IDs after online payment
$payment_id = !empty($_POST['payment_id']) ? sanitizeInput($_POST['payment_id']) : null;
$order_id = !empty($_POST['order_id']) ? sanitizeInput($_POST['order_id']) : null;
// Status — prefer provided valid status, else infer
$clientStatus = isset($_POST['status']) ? sanitizeInput($_POST['status']) : null;
$status = in_array($clientStatus, ['pending', 'completed'], true)
? $clientStatus
: ($payment_method === 'online' ? 'completed' : ($payment_method === 'free' ? 'completed' : 'pending'));
// Validate min amount for non-free
if ($payment_method !== 'free' && $amount < 10) {
logError("Invalid camp registration amount: $amount");
echo json_encode(['success' => false, 'message' => 'Amount must be at least ₹10 for non-free registrations']);
exit;
}
// Offline proof upload
$payment_proof = null;
if ($payment_method === 'offline') {
if (!empty($_FILES['payment_proof']['name']) && $_FILES['payment_proof']['error'] === UPLOAD_ERR_OK) {
// Use same folder convention as donation form
$uploadResult = uploadFile($_FILES['payment_proof'], 'img/payments');
if ($uploadResult['success']) {
$payment_proof = $uploadResult['filename'];
} else {
logError('Payment proof upload failed: ' . $uploadResult['message']);
echo json_encode(['success' => false, 'message' => $uploadResult['message']]);
exit;
}
} else {
logError('Missing payment proof for offline payment');
echo json_encode(['success' => false, 'message' => 'Payment proof is required for offline payments']);
exit;
}
}
// Generate unique camp ID
$camp_id = generateUniqueId('CMP');
try {
$stmt = $db->prepare("
INSERT INTO camps (
program, name, father_name, address, class, amount, payment_method,
place, email, payment_id, order_id, payment_proof, status, camp_id, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
");
$stmt->execute([
$program, $name, $father_name, $address, $class, $amount, $payment_method,
$place, $email, $payment_id, $order_id, $payment_proof, $status, $camp_id
]);
echo json_encode([
'success' => true,
'camp_id' => $camp_id,
'amount' => $amount,
'status' => $status
]);
} catch (PDOException $e) {
logError('Camp registration error: ' . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Failed to save registration. Please try again.']);
}
exit;
}
include 'header.php';
include 'navbar.php';
?>
Camp Registration Form