forked from kumar776/Midterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab6.html
282 lines (232 loc) · 8.27 KB
/
Lab6.html
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="bootstrap.css" rel="stylesheet" />
<script src="jquery-3.4.1.js"></script>
</head>
<body>
<div class="container">
<h2>Add Person<h2>
<form>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">id</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="id" placeholder="Id">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Name</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" placeholder="Name">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Age</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="age" placeholder="Age">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Height</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="height" placeholder="Height">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">CNIC</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="cnic" placeholder="CNIC">
</div>
</div>
<div class="form-group row">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<button type="button" class="btn btn-primary" id="addbtn">Add Person</button>
</div>
</div>
</form>
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Height</th>
<th>Cnic</th>
<th></th>
</tr>
</thead>
<tbody id="stdbody">
</tbody>
</table>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Person</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">id</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="eid" placeholder="Id">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Name</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="ename" placeholder="Name">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Age</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="eage" placeholder="Age">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">Height</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="eheight" placeholder="Height">
</div>
</div>
<div class="form-group row">
<lable class="col-sm-2 col-form-lable">CNIC</lable>
<div class="col-sm-10">
<input type="text" class="form-control" id="ecnic" placeholder="CNIC">
</div>
</div>
<div class="form-group row">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<button type="button" class="btn btn-primary" id="addbtn">Add Person</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" id="updateButton" class="btn btn-primary">Update changes</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$("#addbtn").click(function (){
var id = $("#id").val();
var name = $("#name").val();
var age = $("#age").val();
var height = $("#height").val();
var cnic = $("#cnic").val();
var jsonObj = { ID: id , Name: name , Age: age , Height: height , CNIC: cnic};
$.ajax({
type: "POST",
url: "http://labapi.somee.com/api/Person",
data: jsonObj,
success: function(result){
$("#stdbody").append("<tr><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td><td>" + height + "</td><td>" + cnic + "</td></tr>")
alert("Success");
},
failure: function(result){
alert("Failure");
},
error: function(result){
alert("Error");
},
});
});
$("#updateButton").click(function (){
var id = $("#eid").val();
var name = $("#ename").val();
var age = $("#eage").val();
var height = $("#eheight").val();
var cnic = $("#ecnic").val();
var jsonObj = { ID: id , Name: name , Age: age , Height: height , CNIC: cnic};
var Url = "http://labapi.somee.com/api/Person/" + id;
$.ajax({
type: "PUT",
url: Url,
data: JSON.stringify(jsonObj),
contentType : "application/json;charset=utf-8",
success: function(res){
$("#stdbody").html("");
for (r in res) {
$("#stdbody").append("<tr><td>" + res[r].ID + "</td><td>" + res[r].Name + "</td><td>" + res[r].Age + "</td><td>" + res[r].Height + "</td><td>" + res[r].CNIC + "</td><td><button type='button' class='btn btn-info' data-toggle='modal' data-target='#exampleModal' onclick=editPerson('" + res[r].ID + "')>Edit</button> | <button type='button' class='btn btn-danger'>danger</button> </td></tr>")
}
},
failure: function(result){
alert("Failure");
},
error: function(result){
alert("Error");
},
});
});
getPersonData();
});
function getPersonData(){
$("#stdbody").html("");
$.ajax({
type: "GET",
url: "http://labapi.somee.com/api/Person",
success: function (res) {
for (r in res) {
$("#stdbody").append("<tr><td>" + res[r].ID + "</td><td>" + res[r].Name + "</td><td>" + res[r].Age + "</td><td>" + res[r].Height + "</td><td>" + res[r].CNIC + "</td><td><button type='button' class='btn btn-info' data-toggle='modal' data-target='#exampleModal' onclick=editPerson('" + res[r].ID + "')>Edit</button> | <button type='button' class='btn btn-danger' onclick=deletePerson('" + res[r].ID + "')>Delete</button> </td></tr>")
}
},
failure: function (res) {
alert("Failure");
},
error: function (res) {
alert("Error");
}
});
}
function editPerson(id){
var editUrl = "http://labapi.somee.com/api/Person/" + id;
$.ajax({
type: "GET",
url: editUrl,
success: function (res) {
$("#eid").val(res.ID);
$("#ename").val(res.Name);
$("#eage").val(res.Age);
$("#eheight").val(res.Height);
$("#ecnic").val(res.CNIC);
},
failure: function (res) {
alert("Failure");
},
error: function (res) {
alert("Error");
}
});
}
function deletePerson(id){
var delUrl = "http://labapi.somee.com/api/Person/" + id;
$.ajax({
type: "DELETE",
url: delUrl,
success: function (res) {
getPersonData();
},
failure: function (res) {
alert("Failure");
},
error: function (res) {
alert("Error");
}
});
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>