-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.php
More file actions
148 lines (129 loc) · 7.58 KB
/
form.php
File metadata and controls
148 lines (129 loc) · 7.58 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
<!--AHMAD ARIF AIMAN B. AHMAD FAUZI | 2113419
INFO4345/S1 WEB APP SECURITY -->
<?php
require 'session_checks.php';
require 'security_config.php';
startSecureSession(); $csrfToken = generateCsrfToken();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignment</title>
<!--css-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<!--js-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="js/validation.js"></script>
</head>
<body data-bs-spy="scroll" data-bs-target="#navbar" data-bs-offset="70" class="scrollspy" tabindex="0">
<nav id="navbar" class="navbar navbar-light bg-light fixed justify-content-end">
<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link" href="#section1">Student Details</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Student Information</a>
</li>
<li>
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</nav>
<section id="section1" style="margin: 50px 0;">
<h1 style="text-align: center;margin: 20px 0;" >A. Student Details</h1>
<div class="container">
<form id="form" action="addData.php" method="post" onsubmit="return validation()">
<input type="hidden" name="csrf_token" value="<?php echo $csrfToken; ?>">
<div class="row text-align">
<div class="form-group col-lg-2">
<label for="name" class="form-label">Name (Legal/Official):</label>
<input type="text" id="name" name="name" class="form-control" pattern="[A-Za-z].{1,}" required>
</div>
<div class="form-group col-lg-2">
<label for="matric-no" class="form-label">Matric No:</label>
<input type="text" id="matric-no" name="matric_no" class="form-control" pattern="^\d{7}$" required>
</div>
<div class="form-group"><br>
<label for="current-address" class="form-label">Current Address:</label>
<textarea id="current-address" name="current_address" class="form-control" pattern="[A-Za-z0-9\s,.]+(?:\d{5})?.*" required></textarea>
</div>
<div class="form-group"><br>
<label for="home-address" class="form-label">Home Address:</label>
<textarea id="home-address" name="home_address" class="form-control" pattern="[A-Za-z0-9\s,.]+(?:\d{5})?.*" required></textarea>
</div>
<div class="form-group"><br>
<label for="email" class="form-label">Email (Gmail Account):</label>
<input type="email" id="email" name="email" class="form-control" pattern="[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$" required>
</div>
<div class="form-group col-lg-2"><br>
<label for="mobile-phone" class="form-label">Mobile Phone No:</label>
<input type="tel" id="mobile-phone" name="mobile_phone" class="form-control" pattern="[0-9].{10,}" required>
</div>
<div class="form-group col-lg-3"><br>
<label for="home-phone" class="form-label">Home Phone No (Emergency):</label>
<input type="tel" id="home-phone" name="home_phone" class="form-control" pattern="[0-9].{10,}" required>
</div>
<div class="form-group" style="display: grid; align-items: flex-end; margin-top: 20px;">
<input type="submit" name="action" id="formSubmit" value="submit" class="btn btn-primary" required>
</div>
</div>
</form>
</div>
</section>
<section id="section2" style="margin: 50px 0;">
<h1 style="text-align: center;margin: 50px 0;" >B. Student Information</h1>
<div class="container">
<table class="table table-hover">
<tbody id="data-container">
<script>
// Fetch data initially and after any data modification
function fetchData() {
fetch('crud.php')
.then(response => response.text())
.then(html => document.getElementById('data-container').innerHTML = html)
.catch(error => console.error('Failed to load data:', error));
}
function performAction(action, id) {
if (action === 'delete' && !confirm('Are you sure you want to delete this record?')) {
return;
}
const formData = new FormData();
formData.append('action', action);
formData.append('id', id);
// Collect form data if action is 'edit'
if (action === 'edit') {
formData.append('name', document.getElementById('name' + id).value);
formData.append('matric_no', document.getElementById('matric_no' + id).value);
formData.append('current_address', document.getElementById('current_address' + id).value);
formData.append('home_address', document.getElementById('home_address' + id).value);
formData.append('email', document.getElementById('email' + id).value);
formData.append('mobile_phone', document.getElementById('mobile_phone' + id).value);
formData.append('home_phone', document.getElementById('home_phone' + id).value);
}
// Send data to server
fetch('crud.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(result => {
alert('Updated successfully');
fetchData(); // Reload data to reflect changes
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while updating the record. Please try again.');
});
}
// Initial data fetch
document.addEventListener('DOMContentLoaded', fetchData);
</script>
</tbody>
</table>
</div>
</section>
</body>
</html>