-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_contact.php
More file actions
84 lines (69 loc) · 2.64 KB
/
Copy pathprocess_contact.php
File metadata and controls
84 lines (69 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// Include database connection
require_once 'config/database.php';
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Set default response
$response = [
'success' => false,
'message' => 'An error occurred while processing your request.'
];
// Check if form is submitted via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get form data
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
// Log received data for debugging
error_log("Received form data: " . json_encode($_POST));
// Validate input
$errors = [];
if (empty($name)) {
$errors[] = 'Name is required';
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Valid email is required';
}
if (empty($subject)) {
$errors[] = 'Subject is required';
}
if (empty($message)) {
$errors[] = 'Message is required';
}
// If no errors, save to database
if (empty($errors)) {
try {
// Prepare the query
$stmt = $pdo->prepare('INSERT INTO contact_messages (name, email, subject, message, status, created_at) VALUES (?, ?, ?, ?, ?, NOW())');
// Execute the query with the form data
$result = $stmt->execute([$name, $email, $subject, $message, 'new']);
if ($result) {
$response = [
'success' => true,
'message' => 'Thank you for your message. We will get back to you soon!'
];
// Log successful insertion
error_log("Message successfully inserted: " . $pdo->lastInsertId());
}
} catch (PDOException $e) {
// Log the error (avoid exposing details in production)
error_log('Contact form error: ' . $e->getMessage());
$response = [
'success' => false,
'message' => 'Database error occurred. Please try again later.',
'debug' => $e->getMessage() // Remove in production
];
}
} else {
$response = [
'success' => false,
'message' => 'Please fix the following errors:',
'errors' => $errors
];
}
}
// Return JSON response
header('Content-Type: application/json');
echo json_encode($response);