-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_contact.php
More file actions
47 lines (39 loc) · 1.35 KB
/
process_contact.php
File metadata and controls
47 lines (39 loc) · 1.35 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
<?php
// process_contact.php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Collect and sanitize data
$FullName = trim($_POST['FullName']);
$Email = trim($_POST['Email']);
$Phone = trim($_POST['Phone']);
$Subject = trim($_POST['Subject']);
$Message = trim($_POST['Message']);
// Validate required fields
if (empty($FullName) || empty($Email) || empty($Message)) {
// Handle error
echo "Please fill in all required fields.";
exit;
}
// Validate email format
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
exit;
}
// Optional fields handling
$Phone = !empty($Phone) ? $Phone : NULL;
$Subject = !empty($Subject) ? $Subject : NULL;
// Prepare and bind
$stmt = $conn->prepare("INSERT INTO contact_messages (FullName, Email, Phone, Subject, Message) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $FullName, $Email, $Phone, $Subject, $Message);
if ($stmt->execute()) {
// Success message
echo "<script>alert('Your message has been sent successfully.'); window.location.href = 'contact.php';</script>";
} else {
// Handle error
echo "Error inserting message: " . $stmt->error;
}
// Close statement and connection
$stmt->close();
$conn->close();
}
?>