-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_contact.php
More file actions
175 lines (157 loc) · 6.83 KB
/
process_contact.php
File metadata and controls
175 lines (157 loc) · 6.83 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
// Include the configuration file
require_once 'includes/config.php';
// Set headers for JSON response
header('Content-Type: application/json');
try {
// Create PDO connection using configuration from config.php
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8", DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Check if form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Sanitize and validate input
$full_name = filter_var(trim($_POST['full_name']), FILTER_SANITIZE_STRING);
$email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
$phone_number = filter_var(trim($_POST['phone_number']), FILTER_SANITIZE_STRING);
$country = filter_var(trim($_POST['country']), FILTER_SANITIZE_STRING);
$subject = filter_var(trim($_POST['subject']), FILTER_SANITIZE_STRING);
$message = filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING);
// Additional data
//$ip_address = $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
// $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$submission_date = date('Y-m-d H:i:s');
$status = 'unread'; // Default status
// Validate required fields
if (empty($full_name) || empty($email) || empty($phone_number) || empty($country) || empty($subject) || empty($message)) {
throw new Exception('All required fields must be filled.');
}
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address.');
}
// Prepare SQL statement for your table structure
$sql = "INSERT INTO contact_submissions (
full_name,
email,
phone_number,
country,
subject,
message,
submission_date,
status
) VALUES (
:full_name,
:email,
:phone_number,
:country,
:subject,
:message,
:submission_date,
:status
)";
$stmt = $pdo->prepare($sql);
// Bind parameters
$stmt->bindParam(':full_name', $full_name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':phone_number', $phone_number, PDO::PARAM_STR);
$stmt->bindParam(':country', $country, PDO::PARAM_STR);
$stmt->bindParam(':subject', $subject, PDO::PARAM_STR);
$stmt->bindParam(':message', $message, PDO::PARAM_STR);
$stmt->bindParam(':submission_date', $submission_date, PDO::PARAM_STR);
$stmt->bindParam(':status', $status, PDO::PARAM_STR);
// $stmt->bindParam(':ip_address', $ip_address, PDO::PARAM_STR);
// $stmt->bindParam(':user_agent', $user_agent, PDO::PARAM_STR);
// Execute the statement
if ($stmt->execute()) {
$submission_id = $pdo->lastInsertId();
// Send email notification (optional)
sendEmailNotification($full_name, $email, $phone_number, $subject, $message);
// Return success response
echo json_encode([
'success' => true,
'message' => 'Thank you! Your message has been sent successfully.',
'submission_id' => $submission_id,
'data' => [
'full_name' => $full_name,
'email' => $email,
'phone_number' => $phone_number,
'country' => $country
]
]);
exit;
} else {
throw new Exception('Failed to save your message. Please try again.');
}
} else {
throw new Exception('Invalid request method.');
}
} catch (Exception $e) {
// Return error response
echo json_encode([
'success' => false,
'message' => 'Error: ' . $e->getMessage()
]);
exit;
}
function sendEmailNotification($name, $email, $phone, $subject, $message) {
// Email configuration
$to = "info@majogoorangers.com";
$email_subject = "New Contact Form Submission: " . $subject;
$email_body = "
<html>
<head>
<title>New Contact Submission</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background-color: #1a5d2c; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; background-color: #f9f9f9; }
.field { margin-bottom: 15px; }
.field-label { font-weight: bold; color: #333; }
.field-value { color: #666; }
</style>
</head>
<body>
<div class='container'>
<div class='header'>
<h1>New Contact Form Submission</h1>
</div>
<div class='content'>
<div class='field'>
<span class='field-label'>Name:</span>
<span class='field-value'> $name</span>
</div>
<div class='field'>
<span class='field-label'>Email:</span>
<span class='field-value'> $email</span>
</div>
<div class='field'>
<span class='field-label'>Phone:</span>
<span class='field-value'> $phone</span>
</div>
<div class='field'>
<span class='field-label'>Subject:</span>
<span class='field-value'> $subject</span>
</div>
<div class='field'>
<span class='field-label'>Message:</span>
<div class='field-value' style='margin-top: 10px; padding: 10px; background-color: white; border-left: 3px solid #d4af37;'>$message</div>
</div>
<hr>
<p style='color: #777; font-size: 12px;'>
This message was sent from the Majogoo Rangers contact form at " . date('Y-m-d H:i:s') . ".
</p>
</div>
</div>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Majogoo Rangers Contact Form <noreply@majogoorangers.com>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
// Send email
@mail($to, $email_subject, $email_body, $headers);
}
?>