-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
307 lines (263 loc) · 8.39 KB
/
index.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>PHP & Ajax CRUD</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<table id="main" border="0" cellspacing="0">
<tr>
<td id="header">
<h1>PHP REST API CRUD</h1>
<div id="search-bar">
<label>Search :</label>
<input type="text" id="search" autocomplete="off">
</div>
</td>
</tr>
<tr>
<td id="table-form">
<form id="addForm">
Name : <input type="text" name="sname" id="sname">
Age : <input type="number" name="sage" id="sage">
City : <input type="text" name="scity" id="scity">
<input type="submit" id="save-button" value="Save">
</form>
</td>
</tr>
<tr>
<td id="table-data">
<table width="100%" cellpadding="10px" >
<tr>
<th width="40px">Id</th>
<th>Name</th>
<th width="50px">Age</th>
<th width="150px">City</th>
<th width="60px">Edit</th>
<th width="70px">Delete</th>
</tr>
<tbody id="load-table">
</tbody>
</table>
</td>
</tr>
</table>
<div id="error-message" class="messages"></div>
<div id="success-message" class="messages"></div>
<!-- Popup Modal Box for Update the Records BY Default display None-->
<div id="modal">
<div id="modal-form">
<h2>Edit Form</h2>
<form action="" id="edit-form">
<table cellpadding="10px" width="100%">
<tr>
<td width="90px">First Name</td>
<td><input type="text" name="sname" id="edit-name">
<input type="text" name="sid" id="edit-id" hidden="">
</td>
</tr>
<tr>
<td>Age</td>
<td><input type="number" name="sage" id="edit-age"></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="scity" id="edit-city"></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="edit-submit" value="Update"></td>
</tr>
</table>
</form>
<div id="close-btn">X</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//Fetch All Records
function loadTable(){
$("#load-table").html(""); //starting me empty ho jae
$.ajax({
url: "http://localhost/php-rest-api/api-fetch-all.php",
type: "GET",
success:function(data){
//console.log(data);
if(data.status==false){ //read json key
$("#load-table").append("<tr><td colspan='6'><h2>"+data.message+"</h2></td></tr>");
}else{
$.each(data,function(key,value){
//console.log(key+ " "+value.id);
//console.table(data);
$("#load-table").append("<tr>"+
"<td>"+value.id+"</td>"+
"<td>"+value.student_name+"</td>"+
"<td>"+value.age+"</td>"+
"<td>"+value.city+"</td>"+
"<td class='center'><button class='edit-btn' data-eid='"+value.id+"'>Edit</button></td>"+
"<td class='center'><button class='delete-btn' data-id='"+value.id+"'>Delete</button></td>"+
"</tr>");
});
}
}
});
}
loadTable();
//Show Success or Error message
function Message(message,status){
if(status==true){
$("#success-message").html(message).slideDown();
$("#error-message").slideUp();
setTimeout(function(){
$("#success-message").slideUp();
},4000);
}else if(status==false){
$("#error-message").html(message).slideDown();
$("#success-message").slideUp();
setTimeout(function(){
$("#error-message").slideUp();
},4000);
}
}
//Function For Form Data To Json Object
function JsonData(targetForm){
var arr=$(targetForm).serializeArray(); //Now convert All form values into an array
//console.log(arr); //name: "sname", value: "prince"
var obj1={}; // Create empty object
for(i=0; i<arr.length; i++){
//validation: check values empty or not
if(arr[i].value == ""){
return false;
}
obj1[arr[i].name] = arr[i].value; //insert values to object
//console.log(arr[i].name); //sname,sage,scity=="prince","23","India"
}
//console.log(obj1); //Now We have javascript object
var json_string=JSON.stringify(obj1);
//console.log(json_string); //Now convert javascript object to Json Object
return json_string;
}
//Insert New Record
$("#save-button").on("click",function(e){
e.preventDefault(); //Refresh submit functionality off
var jsonobj=JsonData("#addForm"); //Get Json data Of Form Values
//console.log(jsonobj);
if(jsonobj==false){
Message("All Fields Are Required",false); //Call Message Function
}else{
$.ajax({
url: "http://localhost/php-rest-api/api-insert.php",
type: "POST",
data: jsonobj,
success:function(data){
Message(data.message,data.status);
if(data.status==true){
loadTable(); //table reload
$("#addform").trigger('reset'); //form empty ho jayega
}
}
});
}
});
//Delete Record
$(document).on("click",".delete-btn",function(){
if(confirm("Do you Really Want to delete This Record ?")){
var studentid=$(this).data("id"); //get id of partiular student
var obj={sid: studentid}; //create javascript object
var myJSON=JSON.stringify(obj); //convert javscript object to json obj
var row=this; //For closest animation
$.ajax({
url: "http://localhost/php-rest-api/api-delete.php",
type: "POST",
data: myJSON,
success:function(data){
Message(data.message,data.status);
if(data.status==true){
$(row).closest("tr").fadeOut();
//loadTable();
}
}
});
}
});
//Fetch Single Record : Show in Modal Box
$(document).on("click",".edit-btn",function(){
//console.log("triger testing ");
$("#modal").show(); //set css to display block to model box
var studentid=$(this).data("eid"); //Get data attribute value
var obj={sid: studentid}; //First convert to javascript obj
var myJSON=JSON.stringify(obj); //Then convert to json obj
//console.log(myJSON);
$.ajax({
url: "http://localhost/php-rest-api/api-fetch-single.php",
type: "POST",
data: myJSON,
success:function(data){
//console.table(data); //array index [0]
$("#edit-id").val(data[0].id);
$("#edit-name").val(data[0].student_name);
$("#edit-age").val(data[0].age);
$("#edit-city").val(data[0].city);
}
});
});
//Hide Modal Box
$("#close-btn").on("click",function(){
//console.log("triger testing");
$("#modal").hide(); //set css to display none to model box
});
//Update Record
$("#edit-submit").on("click",function(e){
e.preventDefault(); //Refresh by default functionality off
var jsonobj=JsonData("#edit-form"); //get json data using call function
//console.log(jsonobj);
if(jsonobj==false){
Message("All Fields Are Required",false); //call message function
}else{
$.ajax({
url: "http://localhost/php-rest-api/api-update.php",
type: "POST",
data: jsonobj,
success:function(data){
Message(data.message,data.status);
if(data.status==true){
$("#modal").hide();
loadTable(); //table reload
}
}
});
}
});
//Live Search Record =Key Up Event Call==URL se append krkr bhejegy data
$("#search").on("keyup",function(){
$("#load-table").html("");
var search_term=$(this).val();
$.ajax({
url: "http://localhost/php-rest-api/api-search-get.php?search="+ search_term,
type: "GET",
success: function(data){
if(data.status==false){
$("#load-table").append("<tr><td colspan='6'><h2>"+data.message+"</h2></td></tr>");
}else{
$.each(data,function(key,value){
$("#load-table").append("<tr>"+
"<td>"+value.id+"</td>"+
"<td>"+value.student_name+"</td>"+
"<td>"+value.age+"</td>"+
"<td>"+value.city+"</td>"+
"<td class='center'><button class='edit-btn' data-eid='"+value.id+"'>Edit</button></td>"+
"<td class='center'><button class='delete-btn' data-id='"+value.id+"'>Delete</button></td>"+
"</tr>");
});
}
}
});
});
});
</script>
</body>
</html>