-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_customer.php
179 lines (155 loc) · 6.61 KB
/
edit_customer.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
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
176
177
178
179
<?php include "header.php";
require_once('./dao/customerDAO.php');
require_once('WebsiteUser.php');
session_start();
session_regenerate_id(false);
//echo "Session ID: " . session_id() . "\n";
if(isset($_SESSION['websiteUser'])){
if(!$_SESSION['websiteUser']->isAuthenticated()){
header('Location:login.php');
}
} else {
header('Location:login.php');
}
if (!isset($_GET['customerId'])) {
header("Location: index.php");
exit;
} else {
$customerDAO = new customerDAO();
$customer = $customerDAO->getCustomer($_GET['customerId']);
if ($customer) {
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Week 11 - Edit Customer - <?php echo $customer->getCustomerName(); ?></title>
<script type="text/javascript">
function confirmDelete(customerName) {
return confirm("Do you wish to delete " + customerName + "?");
}
</script>
</head>
<body>
<?php
if (isset($_GET['recordsUpdated'])) {
if (is_numeric($_GET['recordsUpdated'])) {
echo '<p style="color: green; text-align: center"> ' . $_GET['recordsUpdated'] . ' Customer Record Updated.</p>';
}
}
if (isset($_GET['missingFields'])) {
if ($_GET['missingFields']) {
echo '<h3 style="color:red;"> Please enter both first and last names.</h3>';
}
}
?>
<br>
<h3 style="text-align: center">Edit Customer</h3>
<?php
$errorMessages = Array();
$hasError = false;
if (isset($_POST['customerName']) ||
isset($_POST['phoneNumber']) ||
isset($_POST['emailAddress'])) {
$action = '';
$redirect = 'f';
if ($_POST['customerName'] == "") {
$hasError = true;
$errorMessages['customerNameError'] = 'Please enter a Name.';
} else if (!preg_match("/^[a-zA-Z ,.'\\-]+$/", $_POST['customerName'])) {
$hasError = true;
$errorMessages['invalidName'] = 'Name can only contain alphabetic characters.';
}
if ($_POST['phoneNumber'] == "") {
$errorMessages['phoneNumberError'] = "Please enter a Phone number.";
$hasError = true;
} else if (!preg_match("/(^[0-9]{10}$)/", $_POST['phoneNumber'])) {
$hasError = true;
$errorMessages['invalidNumber'] = 'Phone Number is invalid.';
}
if ($_POST['emailAddress'] == "") {
$errorMessages['emailAddressError'] = "Please enter a Email Address.";
$hasError = true;
}
}
if($hasError){
$redirect = "t";
}
else{
$redirect = "f";
}
if(isset($redirect) && $redirect == "t") {
$action = "process_customer.php?action=edit";
} else {
$action = "#";
}
?>
<form name="editCustomer" method="post" action="<?php echo $action; ?>">
<table style="margin:20px auto 20px auto">
<tr>
<td>Customer ID:</td>
<td><input type="hidden" name="customerId" id="customerId"
value="<?php echo $customer->getCustomerId(); ?>"><?php echo $customer->getCustomerId(); ?>
</td>
</tr>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="customerName" id="customerName"
value="<?php echo $customer->getCustomerName(); ?>">
<?php
if(isset($errorMessages['customerNameError'])){
echo "<br><span style='color:red; font-size:12px'>" . $errorMessages['customerNameError'] . "</span>";
}
else if(isset($errorMessages['invalidName'])){
echo "<br><span style='color:red; font-size:12px'>" . $errorMessages['invalidName'] . "</span>";
}
?>
</td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phoneNumber" id="phoneNumber"
value="<?php echo $customer->getPhoneNumber(); ?>">
<?php
if(isset($errorMessages['phoneNumberError'])){
echo "<br><span style='color:red; font-size:12px'>" . $errorMessages['phoneNumberError'] . "</span>";
}
else if(isset($errorMessages['invalidNumber'])){
echo "<br><span style='color:red; font-size:12px'>" . $errorMessages['invalidNumber'] . "</span>";
}
?>
</td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="emailAddress" id="emailAddress"
value="<?php echo $customer->getEmailAddress(); ?>">
<?php
if(isset($errorMessages['emailAddressError'])){
echo "<br><span style='color:red; font-size:12px'>" . $errorMessages['emailAddressError'] . "</span>";
}
?>
</td>
</tr>
<tr>
<td colspan="2"><a onclick="return confirmDelete('<?php echo $customer->getCustomerName(); ?>')"
href="process_customer.php?action=delete&customerId=<?php echo $customer->getCustomerId(); ?>">DELETE <?php echo $customer->getCustomerName(); ?></a>
</td>
</tr>
<tr>
<td><input type="submit" name="btnSubmit" id="btnSubmit" value="Update Customer"></td>
<td><input type="reset" name="btnReset" id="btnReset" value="Reset to original"></td>
</tr>
</table>
</form>
<p style="text-align: center; font-size: 15px"><a href="index.php">Back to main page</a></p>
<p style='text-align: center'><a href='logout.php'>Logout!</a></p>
</body>
</html>
<?php
} else {
header("Location: index.php");
exit;
}
} ?>
<?php include "footer.php" ?>