35995-vm/submit.php
Flatlogic Bot fd6af01e3c v1
2025-11-22 11:23:43 +00:00

64 lines
2.4 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'mail/MailService.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fullname = trim($_POST['fullname'] ?? '');
$email = trim($_POST['email'] ?? '');
$mobile_no = trim($_POST['mobile_no'] ?? '');
$job_description = trim($_POST['job_description'] ?? '');
// Validation
if (empty($fullname) || empty($email) || empty($mobile_no)) {
$_SESSION['error_message'] = 'Full Name, Email, and Mobile Number are required.';
header('Location: index.php');
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$_SESSION['error_message'] = 'Invalid email format.';
header('Location: index.php');
exit;
}
$edit_token = bin2hex(random_bytes(32));
try {
$pdo = db();
$stmt = $pdo->prepare(
'INSERT INTO registrations (fullname, email, mobile_no, job_description, edit_token) VALUES (?, ?, ?, ?, ?)'
);
$stmt->execute([$fullname, $email, $mobile_no, $job_description, $edit_token]);
$_SESSION['success_message'] = 'Registration successful!';
// Send admin notification
$admin_email = getenv('MAIL_TO') ?: 'admin@example.com'; // Fallback
$admin_subject = 'New Registration Submitted';
$admin_body = "<p>A new registration has been submitted:</p>
<ul>
<li><strong>Name:</strong> " . htmlspecialchars($fullname) . "</li>
<li><strong>Email:</strong> " . htmlspecialchars($email) . "</li>
<li><strong>Mobile:</strong> " . htmlspecialchars($mobile_no) . "</li>
</ul>";
MailService::sendMail($admin_email, $admin_subject, $admin_body, strip_tags($admin_body));
// Send user the edit link
$edit_link = "http://{$_SERVER['HTTP_HOST']}/edit.php?token={$edit_token}";
$user_subject = 'Your Registration Details';
$user_body = "<p>Thank you for registering.</p>
<p>You can edit or delete your registration using this link: <a href='{$edit_link}'>{$edit_link}</a></p>";
MailService::sendMail($email, $user_subject, $user_body, strip_tags($user_body));
} catch (PDOException $e) {
error_log($e->getMessage());
$_SESSION['error_message'] = 'An error occurred. Please try again.';
}
header('Location: index.php');
exit;
}