-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
245 lines (201 loc) · 10.2 KB
/
profile.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta tags for character set, compatibility, and viewport settings -->
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Includes external PHP file for links (e.g., CSS, JS files) -->
<?php require('inc/links.php'); ?>
<!-- Page title, dynamically fetching site title from settings -->
<title><?php echo $settings_r['site_title'] ?> - PROFILE</title>
</head>
<body class="bg-light">
<!-- Includes header file (e.g., navigation bar) -->
<?php
require('inc/header.php');
// Check if the user is logged in, if not redirect to index page
if(!(isset($_SESSION['login']) && $_SESSION['login']==true)){
redirect('index.php');
}
// SQL query to check if the user exists
$u_exist = select("SELECT * FROM `user_cred` WHERE `id`=? LIMIT 1",[$_SESSION['uId']],'s');
// If the user doesn't exist, redirect to index page
if(mysqli_num_rows($u_exist)==0){
redirect('index.php');
}
// Fetch the user's data
$u_fetch = mysqli_fetch_assoc($u_exist);
?>
<!-- Main content container -->
<div class="container">
<div class="row">
<!-- Section displaying the page title and breadcrumb -->
<div class="col-12 my-5 px-4">
<h2 class="fw-bold">PROFILE</h2>
<div style="font-size: 14px;">
<a href="index.php" class="text-secondary text-decoration-none">HOME</a>
<span class="text-secondary"> > </span>
<a href="#" class="text-secondary text-decoration-none">PROFILE</a>
</div>
</div>
<!-- Section for updating basic user information -->
<div class="col-12 mb-5 px-4">
<div class="bg-white p-3 p-md-4 rounded shadow-sm">
<form id="info-form">
<h5 class="mb-3 fw-bold">Basic Information</h5>
<div class="row">
<!-- Form fields for name, phone number, date of birth, pincode, and address -->
<div class="col-md-4 mb-3">
<label class="form-label">Name</label>
<input name="name" type="text" value="<?php echo $u_fetch['name'] ?>" class="form-control shadow-none" required>
</div>
<div class="col-md-4 mb-3">
<label class="form-label">Phone Number</label>
<input name="phonenum" type="number" value="<?php echo $u_fetch['phonenum'] ?>" class="form-control shadow-none" required>
</div>
<div class="col-md-4 mb-3">
<label class="form-label">Date of birth</label>
<input name="dob" type="date" value="<?php echo $u_fetch['dob'] ?>" class="form-control shadow-none" required>
</div>
<div class="col-md-4 mb-3">
<label class="form-label">Pincode</label>
<input name="pincode" type="number" value="<?php echo $u_fetch['pincode'] ?>" class="form-control shadow-none" required>
</div>
<div class="col-md-8 mb-4">
<label class="form-label">Address</label>
<textarea name="address" class="form-control shadow-none" rows="1" required><?php echo $u_fetch['address'] ?></textarea>
</div>
</div>
<button type="submit" class="btn text-white custom-bg shadow-none">Save Changes</button>
</form>
</div>
</div>
<!-- Section for updating the user profile picture -->
<div class="col-md-4 mb-5 px-4">
<div class="bg-white p-3 p-md-4 rounded shadow-sm">
<form id="profile-form">
<h5 class="mb-3 fw-bold">Picture</h5>
<!-- Display current profile picture -->
<img src="<?php echo USERS_IMG_PATH.$u_fetch['profile'] ?>" class="rounded-circle img-fluid mb-3">
<!-- Form field for uploading a new profile picture -->
<label class="form-label">New Picture</label>
<input name="profile" type="file" accept=".jpg, .jpeg, .png, .webp" class="mb-4 form-control shadow-none" required>
<button type="submit" class="btn text-white custom-bg shadow-none">Save Changes</button>
</form>
</div>
</div>
<!-- Section for changing the user's password -->
<div class="col-md-8 mb-5 px-4">
<div class="bg-white p-3 p-md-4 rounded shadow-sm">
<form id="pass-form">
<h5 class="mb-3 fw-bold">Change Password</h5>
<div class="row">
<!-- Form fields for new password and confirming the password -->
<div class="col-md-6 mb-3">
<label class="form-label">New Password</label>
<input name="new_pass" type="password" class="form-control shadow-none" required>
</div>
<div class="col-md-6 mb-4">
<label class="form-label">Confirm Password</label>
<input name="confirm_pass" type="password" class="form-control shadow-none" required>
</div>
</div>
<button type="submit" class="btn text-white custom-bg shadow-none">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<?php require('inc/footer.php'); ?> <!-- Include footer.php file to render the footer section -->
<script>
// Event listener for the information form submission
let info_form = document.getElementById('info-form');
info_form.addEventListener('submit',function(e){
e.preventDefault(); // Prevent default form submission behavior
let data = new FormData(); // Create a new FormData object to hold form data
data.append('info_form',''); // Append a custom identifier to the data
data.append('name',info_form.elements['name'].value); // Append form field values to the data
data.append('phonenum',info_form.elements['phonenum'].value);
data.append('address',info_form.elements['address'].value);
data.append('pincode',info_form.elements['pincode'].value);
data.append('dob',info_form.elements['dob'].value);
let xhr = new XMLHttpRequest(); // Create a new XMLHttpRequest object
xhr.open("POST","ajax/profile.php",true); // Send a POST request to 'profile.php'
// Callback function to handle server response
xhr.onload = function(){
if(this.responseText == 'phone_already'){ // If phone number already registered
alert('error',"Phone number is already registered!"); // Show error alert
}
else if(this.responseText == 0){ // If no changes were made
alert('error',"No Changes Made!"); // Show alert indicating no changes
}
else{
alert('success','Changes saved!'); // Show success alert for saved changes
}
}
xhr.send(data); // Send the form data to the server
});
// Event listener for the profile form submission
let profile_form = document.getElementById('profile-form');
profile_form.addEventListener('submit',function(e){
e.preventDefault(); // Prevent default form submission behavior
let data = new FormData(); // Create a new FormData object to hold form data
data.append('profile_form',''); // Append a custom identifier to the data
data.append('profile',profile_form.elements['profile'].files[0]); // Append the selected file
let xhr = new XMLHttpRequest(); // Create a new XMLHttpRequest object
xhr.open("POST","ajax/profile.php",true); // Send a POST request to 'profile.php'
// Callback function to handle server response
xhr.onload = function()
{
if(this.responseText == 'inv_img'){ // If the uploaded image type is invalid
alert('error',"Only JPG, WEBP & PNG images are allowed!"); // Show error alert
}
else if(this.responseText == 'upd_failed'){ // If image upload failed
alert('error',"Image upload failed!"); // Show error alert
}
else if(this.responseText == 0){ // If update failed
alert('error',"Updation failed!"); // Show error alert
}
else{
window.location.href=window.location.pathname; // Reload the page after successful update
}
}
xhr.send(data); // Send the form data to the server
});
// Event listener for the password change form submission
let pass_form = document.getElementById('pass-form');
pass_form.addEventListener('submit',function(e){
e.preventDefault(); // Prevent default form submission behavior
let new_pass = pass_form.elements['new_pass'].value; // Get the new password value
let confirm_pass = pass_form.elements['confirm_pass'].value; // Get the confirm password value
// Check if the new password and confirm password match
if(new_pass!=confirm_pass){
alert('error','Password do not match!'); // Show error alert if passwords don't match
return false; // Prevent further execution if passwords don't match
}
let data = new FormData(); // Create a new FormData object to hold form data
data.append('pass_form',''); // Append a custom identifier to the data
data.append('new_pass',new_pass); // Append new password value to the data
data.append('confirm_pass',confirm_pass); // Append confirm password value to the data
let xhr = new XMLHttpRequest(); // Create a new XMLHttpRequest object
xhr.open("POST","ajax/profile.php",true); // Send a POST request to 'profile.php'
// Callback function to handle server response
xhr.onload = function()
{
if(this.responseText == 'mismatch'){ // If the passwords don't match
alert('error',"Password do not match!"); // Show error alert
}
else if(this.responseText == 0){ // If the update failed
alert('error',"Updation failed!"); // Show error alert
}
else{
alert('success','Changes saved!'); // Show success alert if changes are saved
pass_form.reset(); // Reset the password form after successful update
}
}
xhr.send(data); // Send the form data to the server
});
</script> <!-- End of JavaScript code for handling form submissions -->
</body> <!-- Closing the body tag -->
</html> <!-- Closing the HTML tag -->