| Server IP : 103.243.232.44 / Your IP : 216.73.216.250 Web Server : LiteSpeed System : Linux server17213-10344.hostycare.online 5.14.0-687.38.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 12 17:19:12 EDT 2026 x86_64 User : iamakash ( 1400) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/iamakash/public_html/meals28/commune/ |
Upload File : |
<?php
// guest_form.php
// Single-file form and handler for commune guests.
// Update DB credentials below before use.
// DEV: show all errors (development only). Remove in production.
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
// Toggle debug SQL printing (set false in production)
$SHOW_DEBUG = true;
define('DB_HOST', 'localhost');
define('DB_USER', 'mealscom_commune');
define('DB_PASS', 'WwcCr.lDuXsirmiE');
define('DB_NAME', 'mealscom_commune');
$uploadDir = __DIR__ . '/uploads'; // make sure this exists and is writable
// Max file size (5 MB)
$maxFileSize = 5 * 1024 * 1024; // 5 MB
// Utility: sanitize filename piece
function sanitize_filename_piece($s) {
$s = trim(mb_strtolower($s));
// Replace non-alphanumeric with underscore
$s = preg_replace('/[^\p{L}\p{N}]+/u', '_', $s);
$s = trim($s, '_');
return $s ?: 'file';
}
// Prepare response variables
$errors = [];
$success = false;
$firstErrorField = null; // will store id/name of first invalid field
// --- Step 2: Handle redirected success ---
if (isset($_GET['success']) && $_GET['success'] == '1') {
$success = true;
$fname = $lname = $doj = $contact_number = $dob = "";
$emergency_contact_name_1 = $emergency_contact_number_1 = "";
$emergency_contact_name_2 = $emergency_contact_number_2 = "";
$commune = $room_number = $floor = "";
$work_proof_type = "";
}
// Helper to mark first error field
function mark_first_error(&$firstErrorField, $fieldName) {
if (!$firstErrorField) $firstErrorField = $fieldName;
}
// Handle POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// SIMPLE CAPTCHA VALIDATION
if (empty($_POST['captcha_input'])) {
$errors[] = "Captcha is required.";
mark_first_error($firstErrorField, 'captcha_input');
} else {
// Since captcha is JS-based, server cannot know the generated numbers.
// So we store the correct answer in a hidden field via JS.
if (!isset($_POST['captcha_answer']) || $_POST['captcha_input'] != $_POST['captcha_answer']) {
$errors[] = "Captcha is incorrect.";
mark_first_error($firstErrorField, 'captcha_input');
}
}
// Collect and trim
$fname = isset($_POST['fname']) ? trim($_POST['fname']) : '';
$lname = isset($_POST['lname']) ? trim($_POST['lname']) : '';
$doj = isset($_POST['doj']) ? trim($_POST['doj']) : '';
$contact_number = isset($_POST['contact_number']) ? trim($_POST['contact_number']) : '';
$dob = isset($_POST['dob']) ? trim($_POST['dob']) : '';
$emergency_contact_name_1 = isset($_POST['emergency_contact_name_1']) ? trim($_POST['emergency_contact_name_1']) : '';
$emergency_contact_number_1 = isset($_POST['emergency_contact_number_1']) ? trim($_POST['emergency_contact_number_1']) : '';
$emergency_contact_name_2 = isset($_POST['emergency_contact_name_2']) ? trim($_POST['emergency_contact_name_2']) : '';
$emergency_contact_number_2 = isset($_POST['emergency_contact_number_2']) ? trim($_POST['emergency_contact_number_2']) : '';
$commune = isset($_POST['commune']) ? trim($_POST['commune']) : '';
$room_number = isset($_POST['room_number']) ? trim($_POST['room_number']) : '';
$floor = isset($_POST['floor']) ? trim($_POST['floor']) : '';
$work_proof_type = isset($_POST['work_proof_type']) ? trim($_POST['work_proof_type']) : '';
// Validate required fields (text/date/phone)
$requiredFields = [
'First name' => $fname,
'Last name' => $lname,
'Date of joining' => $doj,
'Contact number' => $contact_number,
'Date of birth' => $dob,
'Emergency contact name 1' => $emergency_contact_name_1,
'Emergency contact number 1' => $emergency_contact_number_1,
'Emergency contact name 2' => $emergency_contact_name_2,
'Emergency contact number 2' => $emergency_contact_number_2,
'Commune' => $commune,
'Room number' => $room_number,
'Floor' => $floor,
];
// map labels -> field names (for first error focus)
$fieldNamesMap = [
'First name' => 'fname',
'Last name' => 'lname',
'Date of joining' => 'doj',
'Contact number' => 'contact_number',
'Date of birth' => 'dob',
'Emergency contact name 1' => 'emergency_contact_name_1',
'Emergency contact number 1' => 'emergency_contact_number_1',
'Emergency contact name 2' => 'emergency_contact_name_2',
'Emergency contact number 2' => 'emergency_contact_number_2',
'Commune' => 'commune',
'Room number' => 'room_number',
'Floor' => 'floor',
];
foreach ($requiredFields as $label => $val) {
if ($val === '') {
$errors[] = "$label is required.";
mark_first_error($firstErrorField, $fieldNamesMap[$label]);
}
}
// Work status validation
if ($work_proof_type !== 'working' && $work_proof_type !== 'not_working') {
$errors[] = "Work Status must be selected.";
mark_first_error($firstErrorField, 'work_proof_type');
}
// Date validation (YYYY-MM-DD expected from input type="date")
$dateFields = ['doj' => $doj, 'dob' => $dob];
foreach ($dateFields as $label => $val) {
if (!empty($val)) {
$d = DateTime::createFromFormat('Y-m-d', $val);
if (!$d || $d->format('Y-m-d') !== $val) {
$errors[] = ($label === 'doj' ? 'Date of joining' : 'Date of birth') . " must be a valid date (YYYY-MM-DD).";
mark_first_error($firstErrorField, $label);
}
}
}
// Basic phone validation
$phoneFields = [
'Contact number' => $contact_number,
'Emergency contact number 1' => $emergency_contact_number_1,
'Emergency contact number 2' => $emergency_contact_number_2
];
foreach ($phoneFields as $label => $val) {
if (!preg_match('/^[0-9\+\-\s]{6,30}$/', $val)) {
$errors[] = "$label looks invalid. Use digits, +, - and spaces (6-30 chars).";
// map label to field name (approx)
$map = [
'Contact number' => 'contact_number',
'Emergency contact number 1' => 'emergency_contact_number_1',
'Emergency contact number 2' => 'emergency_contact_number_2'
];
mark_first_error($firstErrorField, $map[$label]);
}
}
// Files: document_1 and document_2 must be uploaded
if (!isset($_FILES['document_1']) || $_FILES['document_1']['error'] === UPLOAD_ERR_NO_FILE) {
$errors[] = "Document 1 is required.";
mark_first_error($firstErrorField, 'document_1');
}
if (!isset($_FILES['document_2']) || $_FILES['document_2']['error'] === UPLOAD_ERR_NO_FILE) {
$errors[] = "Document 2 is required.";
mark_first_error($firstErrorField, 'document_2');
}
// If work_proof_type == working then work_proof_file must be uploaded
if ($work_proof_type === 'working') {
if (!isset($_FILES['work_proof_file']) || $_FILES['work_proof_file']['error'] === UPLOAD_ERR_NO_FILE) {
$errors[] = "Work Proof document is required for Working guests.";
mark_first_error($firstErrorField, 'work_proof_file');
}
}
// If no errors so far, validate files and move them
$uploaded1 = null;
$uploaded2 = null;
$uploaded_workproof = null;
if (empty($errors)) {
// Ensure upload directory exists
if (!is_dir($uploadDir) && !mkdir($uploadDir, 0755, true)) {
$errors[] = "Failed to create upload directory.";
mark_first_error($firstErrorField, 'document_1');
}
// Helper to validate & move file (extension-only check)
$moveFile = function($fileInput, $docLabel, $fname, $lname, $uploadDir, $maxFileSize, &$errors) {
if (!isset($fileInput) || $fileInput['error'] !== UPLOAD_ERR_OK) {
$errors[] = "$docLabel upload failed with error code " . ($fileInput['error'] ?? 'unknown');
return null;
}
if ($fileInput['size'] > $maxFileSize) {
$errors[] = "$docLabel exceeds max size of 5 MB.";
return null;
}
// EXTENSION CHECK ONLY (since finfo & mime_content_type may be unavailable)
$ext = strtolower(pathinfo($fileInput['name'], PATHINFO_EXTENSION));
$allowedExt = ['pdf', 'jpg', 'jpeg', 'png'];
if (!in_array($ext, $allowedExt, true)) {
$errors[] = "$docLabel has invalid extension ($ext). Allowed: PDF, JPG, PNG.";
return null;
}
$namePiece = sanitize_filename_piece($fname . '_' . $lname);
$docKey = strtolower(str_replace(' ', '_', preg_replace('/[^A-Za-z0-9]/', '_', $docLabel)));
// keep fixed keys for document1/document2/workproof if provided by caller
if ($docLabel === 'Document 1') $docKey = 'document1';
if ($docLabel === 'Document 2') $docKey = 'document2';
if ($docLabel === 'Work Proof') $docKey = 'workproof';
$timestamp = time();
$newFilename = sprintf("%s_%s_%s.%s", $namePiece, $docKey, $timestamp, $ext);
$dest = rtrim($uploadDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $newFilename;
if (!move_uploaded_file($fileInput['tmp_name'], $dest)) {
$errors[] = "Failed to move uploaded file for $docLabel.";
return null;
}
return $newFilename;
};
// Move the two mandatory documents
$uploaded1 = $moveFile($_FILES['document_1'], 'Document 1', $fname, $lname, $uploadDir, $maxFileSize, $errors);
$uploaded2 = $moveFile($_FILES['document_2'], 'Document 2', $fname, $lname, $uploadDir, $maxFileSize, $errors);
// Move work proof if working
if ($work_proof_type === 'working') {
$uploaded_workproof = $moveFile($_FILES['work_proof_file'], 'Work Proof', $fname, $lname, $uploadDir, $maxFileSize, $errors);
} else {
$uploaded_workproof = 'not_working';
}
}
// If still no errors, insert into DB
if (empty($errors)) {
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_errno) {
$errors[] = "DB connection failed: " . $mysqli->connect_error;
} else {
$sql = "INSERT INTO commune_guests
(fname, lname, doj, contact_number, dob, document_1, document_2,
emergency_contact_name_1, emergency_contact_number_1,
emergency_contact_name_2, emergency_contact_number_2,
commune, room_number, floor, work_proof_type, work_proof_file)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
$errors[] = "DB prepare failed: " . $mysqli->error;
} else {
$stmt->bind_param(
'ssssssssssssssss',
$fname,
$lname,
$doj,
$contact_number,
$dob,
$uploaded1,
$uploaded2,
$emergency_contact_name_1,
$emergency_contact_number_1,
$emergency_contact_name_2,
$emergency_contact_number_2,
$commune,
$room_number,
$floor,
$work_proof_type,
$uploaded_workproof
);
// DEBUG: build readable SQL (development only)
// if (!empty($GLOBALS['SHOW_DEBUG'])) {
// $debug_query = sprintf(
// "INSERT INTO commune_guests (fname,lname,doj,contact_number,dob,document_1,document_2,emergency_contact_name_1,emergency_contact_number_1,emergency_contact_name_2,emergency_contact_number_2,commune,room_number,floor,work_proof_type,work_proof_file) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');",
// addslashes($fname),
// addslashes($lname),
// addslashes($doj),
// addslashes($contact_number),
// addslashes($dob),
// addslashes($uploaded1),
// addslashes($uploaded2),
// addslashes($emergency_contact_name_1),
// addslashes($emergency_contact_number_1),
// addslashes($emergency_contact_name_2),
// addslashes($emergency_contact_number_2),
// addslashes($commune),
// addslashes($room_number),
// addslashes($floor),
// addslashes($work_proof_type),
// addslashes($uploaded_workproof)
// );
// echo "<pre style='background:#111;color:#bada55;padding:8px;border-radius:6px;'>DEBUG SQL QUERY:\n" . htmlspecialchars($debug_query) . "</pre>";
// }
if ($stmt->execute()) {
$success = true;
// close resources
$stmt->close();
$mysqli->close();
// IMPORTANT: do not output anything before header()
header("Location: " . $_SERVER['PHP_SELF'] . "?success=1");
exit;
} else {
$errors[] = "DB execute failed: " . $stmt->error;
// attempt to unlink uploaded files on DB failure
if ($uploaded1 && file_exists($uploadDir . DIRECTORY_SEPARATOR . $uploaded1)) {
@unlink($uploadDir . DIRECTORY_SEPARATOR . $uploaded1);
}
if ($uploaded2 && file_exists($uploadDir . DIRECTORY_SEPARATOR . $uploaded2)) {
@unlink($uploadDir . DIRECTORY_SEPARATOR . $uploaded2);
}
if ($uploaded_workproof && $uploaded_workproof !== 'not_working' && file_exists($uploadDir . DIRECTORY_SEPARATOR . $uploaded_workproof)) {
@unlink($uploadDir . DIRECTORY_SEPARATOR . $uploaded_workproof);
}
}
$stmt->close();
}
$mysqli->close();
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Commune Guest Registration</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
body { font-family: Arial, sans-serif; background:#f7f7f7; padding:20px; }
.container { max-width:760px; margin:0 auto; background:#fff; padding:20px; box-shadow:0 2px 8px rgba(0,0,0,.05); border-radius:8px; }
label { display:block; margin:10px 0 4px; font-weight:600; }
input[type="text"], input[type="date"], input[type="file"], input[type="tel"], select { width:100%; padding:8px; box-sizing:border-box; }
.row { display:flex; gap:10px; }
.row > div { flex:1; }
.error { background:#ffe6e6; color:#900; padding:10px; border-radius:6px; margin-bottom:12px; }
.success { background:#e6ffea; color:#060; padding:10px; border-radius:6px; margin-bottom:12px; }
button { padding:10px 16px; font-size:16px; cursor:pointer; border:0; background:#0073e6; color:#fff; border-radius:6px; }
small { color:#666; }
/* Highlight first errored field */
.error-field {
border: 2px solid red !important;
background: #fff0f0;
}
</style>
</head>
<body>
<div class="container">
<h2>Commune Guest Registration</h2>
<?php if (!empty($errors) && !$firstErrorField): // fallback: show error box if no field focus was set ?>
<div class="error">
<strong>There were problems with your submission:</strong>
<ul>
<?php foreach ($errors as $e): ?>
<li><?=htmlspecialchars($e)?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if ($success): ?>
<script>
document.addEventListener('DOMContentLoaded', function(){
var f = document.querySelector('form');
if (f) f.reset();
// also remove any stored values (for safety)
var inputs = f.querySelectorAll('input,select,textarea');
inputs.forEach(function(i){ i.value = ''; });
});
</script>
<div class="success">
<strong>Guest registered successfully!</strong>
</div>
<?php endif; ?>
<form method="post" enctype="multipart/form-data" novalidate>
<div class="row">
<div>
<label for="fname">First name</label>
<input
id="fname"
name="fname"
type="text"
required
class="<?= ($firstErrorField=='fname') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='fname') ? 'autofocus' : '' ?>
value="<?= isset($fname) ? htmlspecialchars($fname) : '' ?>"
>
</div>
<div>
<label for="lname">Last name</label>
<input
id="lname"
name="lname"
type="text"
required
class="<?= ($firstErrorField=='lname') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='lname') ? 'autofocus' : '' ?>
value="<?= isset($lname) ? htmlspecialchars($lname) : '' ?>"
>
</div>
</div>
<div class="row">
<div>
<label for="doj">Date of Joining</label>
<input
id="doj"
name="doj"
type="date"
required
class="<?= ($firstErrorField=='doj') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='doj') ? 'autofocus' : '' ?>
value="<?= isset($doj) ? htmlspecialchars($doj) : '' ?>"
>
</div>
<div>
<label for="dob">Date of Birth</label>
<input
id="dob"
name="dob"
type="date"
required
class="<?= ($firstErrorField=='dob') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='dob') ? 'autofocus' : '' ?>
value="<?= isset($dob) ? htmlspecialchars($dob) : '' ?>"
>
</div>
</div>
<label for="contact_number">Contact number</label>
<input
id="contact_number"
name="contact_number"
type="tel"
required
class="<?= ($firstErrorField=='contact_number') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='contact_number') ? 'autofocus' : '' ?>
value="<?= isset($contact_number) ? htmlspecialchars($contact_number) : '' ?>"
>
<label for="document_1">Aadhaar - Latest Downloaded PDF (max 5MB)</label>
<input
id="document_1"
name="document_1"
type="file"
accept=".pdf,image/jpeg,image/png"
class="<?= ($firstErrorField=='document_1') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='document_1') ? 'autofocus' : '' ?>
>
<label for="document_2">PAN/Voter ID/DL (PDF / JPG / PNG, max 5MB)</label>
<input
id="document_2"
name="document_2"
type="file"
accept=".pdf,image/jpeg,image/png"
class="<?= ($firstErrorField=='document_2') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='document_2') ? 'autofocus' : '' ?>
>
<label for="work_proof_type">Work Status</label>
<select
id="work_proof_type"
name="work_proof_type"
class="<?= ($firstErrorField=='work_proof_type') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='work_proof_type') ? 'autofocus' : '' ?>
required
>
<option value="">-- Select --</option>
<option value="working" <?= isset($work_proof_type) && $work_proof_type=='working' ? 'selected' : '' ?>>Working</option>
<option value="not_working" <?= isset($work_proof_type) && $work_proof_type=='not_working' ? 'selected' : '' ?>>Not Working</option>
</select>
<label for="work_proof_file">Work Proof (Office ID / Corporate Card)</label>
<input
id="work_proof_file"
name="work_proof_file"
type="file"
accept=".pdf,image/jpeg,image/png"
class="<?= ($firstErrorField=='work_proof_file') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='work_proof_file') ? 'autofocus' : '' ?>
>
<small>If Work Status is "Working", uploading proof is mandatory.</small>
<label for="emergency_contact_name_1">Emergency Contact Name 1</label>
<input
id="emergency_contact_name_1"
name="emergency_contact_name_1"
type="text"
required
class="<?= ($firstErrorField=='emergency_contact_name_1') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='emergency_contact_name_1') ? 'autofocus' : '' ?>
value="<?= isset($emergency_contact_name_1) ? htmlspecialchars($emergency_contact_name_1) : '' ?>"
>
<label for="emergency_contact_number_1">Emergency Contact Number 1</label>
<input
id="emergency_contact_number_1"
name="emergency_contact_number_1"
type="tel"
required
class="<?= ($firstErrorField=='emergency_contact_number_1') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='emergency_contact_number_1') ? 'autofocus' : '' ?>
value="<?= isset($emergency_contact_number_1) ? htmlspecialchars($emergency_contact_number_1) : '' ?>"
>
<label for="emergency_contact_name_2">Emergency Contact Name 2</label>
<input
id="emergency_contact_name_2"
name="emergency_contact_name_2"
type="text"
required
class="<?= ($firstErrorField=='emergency_contact_name_2') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='emergency_contact_name_2') ? 'autofocus' : '' ?>
value="<?= isset($emergency_contact_name_2) ? htmlspecialchars($emergency_contact_name_2) : '' ?>"
>
<label for="emergency_contact_number_2">Emergency Contact Number 2</label>
<input
id="emergency_contact_number_2"
name="emergency_contact_number_2"
type="tel"
required
class="<?= ($firstErrorField=='emergency_contact_number_2') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='emergency_contact_number_2') ? 'autofocus' : '' ?>
value="<?= isset($emergency_contact_number_2) ? htmlspecialchars($emergency_contact_number_2) : '' ?>"
>
<div class="row">
<div>
<label for="commune">Living in Commune</label>
<select
id="commune"
name="commune"
required
class="<?= ($firstErrorField=='commune') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='commune') ? 'autofocus' : '' ?>
>
<option value="">-- Select Commune --</option>
<option value="Commune Sector 45" <?= isset($commune) && $commune=='Commune Sector 45' ? 'selected' : '' ?>>Commune Sector 45</option>
<option value="Commune Quartex" <?= isset($commune) && $commune=='Commune Quartex' ? 'selected' : '' ?>>Commune Quartex</option>
<option value="Commune Anant" <?= isset($commune) && $commune=='Commune Anant' ? 'selected' : '' ?>>Commune Anant</option>
<option value="Commune Greenwood" <?= isset($commune) && $commune=='Commune Greenwood' ? 'selected' : '' ?>>Commune Greenwood</option>
<option value="Commune Prime" <?= isset($commune) && $commune=='Commune Prime' ? 'selected' : '' ?>>Commune Prime</option>
<option value="Commune Florence" <?= isset($commune) && $commune=='Commune Florence' ? 'selected' : '' ?>>Commune Florence</option>
<option value="Commune Nova" <?= isset($commune) && $commune=='Commune Nova' ? 'selected' : '' ?>>Commune Nova</option>
</select>
</div>
<div>
<label for="room_number">Room number</label>
<input
id="room_number"
name="room_number"
type="text"
required
class="<?= ($firstErrorField=='room_number') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='room_number') ? 'autofocus' : '' ?>
value="<?= isset($room_number) ? htmlspecialchars($room_number) : '' ?>"
>
</div>
<div>
<label for="floor">Floor</label>
<input
id="floor"
name="floor"
type="text"
required
class="<?= ($firstErrorField=='floor') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='floor') ? 'autofocus' : '' ?>
value="<?= isset($floor) ? htmlspecialchars($floor) : '' ?>"
>
</div>
</div>
<p><small>All fields are mandatory. Allowed file types: PDF, JPG, PNG. Max file size 5MB each.</small></p>
<label for="captcha_input">Security Check</label>
<div style="margin:5px 0; font-weight:bold;">
Solve: <span id="captcha_question"></span>
</div>
<input
type="text"
id="captcha_input"
name="captcha_input"
class="<?= ($firstErrorField=='captcha_input') ? 'error-field' : '' ?>"
<?= ($firstErrorField=='captcha_input') ? 'autofocus' : '' ?>
placeholder="Enter answer"
required
>
<input type="hidden" id="captcha_answer" name="captcha_answer">
<button type="submit">Submit</button>
</form>
</div>
<script>
// auto-scroll to the first error field (if present)
document.addEventListener("DOMContentLoaded", function(){
var errorField = document.querySelector(".error-field");
if (errorField) {
// slightly delay to ensure autofocus has effect in some browsers
setTimeout(function(){
errorField.scrollIntoView({ behavior: 'smooth', block: 'center' });
// focus if not focused already
if (typeof errorField.focus === 'function') errorField.focus();
}, 100);
}
});
</script>
<script>
let num1 = Math.floor(Math.random() * 10) + 1;
let num2 = Math.floor(Math.random() * 10) + 1;
let answer = num1 + num2;
document.getElementById("captcha_question").innerText = num1 + " + " + num2;
document.querySelector("form").addEventListener("submit", function(e){
document.getElementById("captcha_answer").value = answer;
let userAns = document.getElementById("captcha_input").value.trim();
if (parseInt(userAns) !== answer) {
alert("Incorrect Captcha. Please try again.");
e.preventDefault();
document.getElementById("captcha_input").classList.add("error-field");
document.getElementById("captcha_input").focus();
}
});
</script>
</body>
</html>