-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_contact.php
119 lines (106 loc) · 5.22 KB
/
process_contact.php
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
<html>
<head>
<title>TUMMY FOR YUM-YUM</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/header_footer.css">
<link rel="stylesheet" href="css/process_contact.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php
include "navbar.inc.php";
?>
<article class="formvalidateOutput">
<?php
// Constants for accessing our DB:
define("DBHOST", "161.117.122.252");
define("DBNAME", "p2_5");
define("DBUSER", "p2_5");
define("DBPASS", "rBs4CTxkDU");
$contactName = $email = $contactPhoneNumber = $contactMessage = $errorMsg = "";
$success = true;
if (empty($_POST["contactName"])) {
$errorMsg .= "First name is required.<br>";
$success = false;
} else {
$contactName = sanitize_input($_POST["contactName"]);
if (!preg_match("/^([a-zA-Z]+)$/", $contactName)) {
$errorMsg .= "Please enter a proper first name.<br>";
$success = false;
}
}
if (empty($_POST["email"])) {
$errorMsg .= "Email is required.<br>";
$success = false;
} else {
$email = sanitize_input($_POST["email"]); // Additional check to make sure e-mail address is well-formed.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errorMsg .= "Invalid email format.<br>";
$success = false;
}
}
if (empty($_POST["contactPhoneNumber"])) {
$errorMsg .= "Contact number is required.<br>";
$success = false;
} else {
$contactPhoneNumber = sanitize_input($_POST["contactPhoneNumber"]);
if (!preg_match("/^([0-9]{8})$/", $contactPhoneNumber)) {
$errorMsg .= "Please enter a valid number.<br>";
$success = false;
}
}
if (empty($_POST["contactMessage"])) {
$errorMsg .= "Feed back is required.<br>";
$success = false;
} else {
$contactMessage = sanitize_input($_POST["contactMessage"]);
if (!preg_match("/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/", $contactMessage)) {
$errorMsg .= "Please enter a valid comment.<br>";
$success = false;
}
}
if ($success) {
echo "<h1>Thank you for your feedback!</h1>";
echo "<p>We will get back to you as soon as possible.</p>";
echo "<p>Have a nice day.</p>";
header('Refresh:2; url=index.php');
saveMemberToDB();
} else {
echo '<script>alert("Please check your form and submit again."); </script>';
header('Location: about_contact.php');
}
//Helper function that checks input for malicious or unwanted content.
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function saveMemberToDB() {
global $contactName, $email, $contactPhoneNumber, $contactMessage, $errorMsg;
// Create connection
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
// Check connection
if ($conn->connect_error) {
$errorMsg = "Connection failed: " . $conn->connect_error;
} else {
$sql = "INSERT INTO customer_enquiries (name, email, mobileNumber, enquiryMessage)";
$sql .= " VALUES ('$contactName', '$email', '$contactPhoneNumber', '$contactMessage')";
$compile = $conn->prepare("INSERT INTO customer_enquiries (name, email, mobileNumber, enquiryMessage) VALUES (?, ?, ?, ?)");
$compile->bind_param("ssis", $contactName, $email, $contactPhoneNumber, $contactMessage);
$compile->execute();
$compile->close();
}
$conn->close();
}
?>
</article>
<?php
include "footer.inc.php";
?>
</body>
</html>