more contact form updates
This commit is contained in:
parent
b48a5f8e0c
commit
7a0594d4f5
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
/**
|
||||
* File: ContactController.php
|
||||
* Version: 1.5
|
||||
* Version: 1.6
|
||||
* Path: /app/Controllers/ContactController.php
|
||||
* Purpose: Handles contact form display and submission logic.
|
||||
* Project: Wizdom Networks Website
|
||||
|
|
@ -41,56 +41,55 @@ class ContactController
|
|||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
Response::badRequest('Invalid request method.');
|
||||
}
|
||||
|
||||
|
||||
$data = $_POST;
|
||||
|
||||
// Validate required fields
|
||||
$requiredFields = ['first_name', 'last_name', 'email', 'message'];
|
||||
|
||||
$requiredFields = ['first_name', 'last_name', 'email', 'phone', 'subject', 'message'];
|
||||
foreach ($requiredFields as $field) {
|
||||
if (empty($data[$field])) {
|
||||
Response::badRequest("Missing required field: $field");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Sanitize input
|
||||
$firstName = Sanitizer::sanitizeString($data['first_name']);
|
||||
$lastName = Sanitizer::sanitizeString($data['last_name']);
|
||||
$email = Sanitizer::sanitizeString($data['email']);
|
||||
$phone = Sanitizer::sanitizeString($data['phone']);
|
||||
$subject = Sanitizer::sanitizeString($data['subject']);
|
||||
$message = Sanitizer::sanitizeString($data['message']);
|
||||
|
||||
// Validate email format
|
||||
|
||||
if (!Validator::IsEmail($email)) {
|
||||
Response::badRequest('Invalid email address.');
|
||||
}
|
||||
|
||||
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
|
||||
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'unknown';
|
||||
|
||||
// Create DB connection and save to DB
|
||||
$database = new Database();
|
||||
$pdo = $database->getConnection();
|
||||
|
||||
|
||||
$pdo = (new Database())->getConnection();
|
||||
$contact = new ContactModel($pdo);
|
||||
$result = $contact->save([
|
||||
$result = $contact->saveContactForm([
|
||||
'first_name' => $firstName,
|
||||
'last_name' => $lastName,
|
||||
'email' => $email,
|
||||
'phone' => $phone,
|
||||
'subject' => $subject,
|
||||
'message' => $message,
|
||||
'ip_address' => $ip,
|
||||
'user_agent' => $userAgent,
|
||||
]);
|
||||
|
||||
|
||||
if (!$result) {
|
||||
Logger::error("Failed to save contact form submission for email: $email");
|
||||
Logger::error("Contact form submission failed for email: $email");
|
||||
Response::serverError('An error occurred while submitting your message. Please try again later.');
|
||||
}
|
||||
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'Your message has been successfully submitted. Thank you!'
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Logger::error("Exception during contact form submission: " . $e->getMessage());
|
||||
Logger::error("Exception in contact submission: " . $e->getMessage());
|
||||
Response::serverError('A server error occurred. Please try again later.');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,4 +100,50 @@ class ContactModel
|
|||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Save a full contact form submission to contact_messages table.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function saveContactForm(array $data): bool
|
||||
{
|
||||
try {
|
||||
$sql = "INSERT INTO contact_messages (
|
||||
first_name,
|
||||
last_name,
|
||||
email,
|
||||
phone,
|
||||
subject,
|
||||
message,
|
||||
ip_address,
|
||||
user_agent
|
||||
) VALUES (
|
||||
:first_name,
|
||||
:last_name,
|
||||
:email,
|
||||
:phone,
|
||||
:subject,
|
||||
:message,
|
||||
:ip_address,
|
||||
:user_agent
|
||||
)";
|
||||
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->bindParam(':first_name', $data['first_name']);
|
||||
$stmt->bindParam(':last_name', $data['last_name']);
|
||||
$stmt->bindParam(':email', $data['email']);
|
||||
$stmt->bindParam(':phone', $data['phone']);
|
||||
$stmt->bindParam(':subject', $data['subject']);
|
||||
$stmt->bindParam(':message', $data['message']);
|
||||
$stmt->bindParam(':ip_address', $data['ip_address']);
|
||||
$stmt->bindParam(':user_agent', $data['user_agent']);
|
||||
|
||||
return $stmt->execute();
|
||||
} catch (\Exception $e) {
|
||||
Logger::error("Failed to save contact form: " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -730,14 +730,27 @@
|
|||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<label for="phone" class="pb-2">Phone Number <small class="text-muted">(required to weed out spam)</small></label>
|
||||
<label for="phone" class="pb-2">Phone Number <small class="text-muted">(required to qualify submissions)</small></label>
|
||||
<input type="tel" name="phone" id="phone" class="form-control" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group mt-3">
|
||||
<label for="message" class="pb-2">Message</label>
|
||||
<textarea name="message" id="message" rows="5" class="form-control" required></textarea>
|
||||
|
||||
<div class="row mt-3">
|
||||
<div class="form-group mt-3">
|
||||
<label for="subject">Subject</label>
|
||||
<input
|
||||
type="text"
|
||||
id="subject"
|
||||
name="subject"
|
||||
class="form-control"
|
||||
required
|
||||
placeholder="Subject of your inquiry"
|
||||
>
|
||||
</div>
|
||||
<div class="row mt-3">
|
||||
<label for="message" class="pb-2">Message</label>
|
||||
<textarea name="message" id="message" rows="5" class="form-control" required></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="text-center mt-4">
|
||||
|
|
|
|||
Loading…
Reference in New Issue