-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
183 lines (154 loc) · 4.33 KB
/
index.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
<html>
<head>
<title>Simple task</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<div>
<form>
Task ID (read-only) : <input type="text" name="id" id="input-id" readonly="true"><br>
Task name: <input type="text" name="name" id="input-name"><br>
Task description: <input type="text" name="description" id="input-description"><br>
<span style="background-color: yellow" onclick="insertTask()">INSERT</span>
<span style="background-color: orange" onclick="updateTask()">UPDATE</span>
</form>
</div>
<div>
<h1>CURRENT TASK</h1>
<table id="tasks">
</table>
</div>
</body>
<script>
$( document ).ready(function() {
getTasks();
})
var selectedId = '34';
function getTasks() {
$.ajax({
'type' : 'get',
'url' : window.location.href + 'methods/get_tasks.php',
'success' : function(resp){
try {
newContent = '';
newContent += '<tr><td>Id</td>' +
'<td>Name</td>' +
'<td>Description</td>' +
'<td>Date created</td>' +
'<td>Date updated</td><td></td><td></td></tr>';
ids = '';
for (index = 0; index < resp.length; index++) {
newContent += '<tr><td>' + resp[index].id + '</td>' +
'<td>' + resp[index].name + '</td>' +
'<td>' + resp[index].description + '</td>' +
'<td>' + resp[index].dateCreated + '</td>' +
'<td>' + resp[index].dateUpdated + '</td>' +
"<td><button id=" + resp[index].id + ">EDIT</button></td>" +
"<td><button id=delete-" + resp[index].id + ">DELETE</button></td></tr>";
ids += resp[index].id + ';';
}
$('#tasks').html(newContent);
ids = ids.split(';');
for (index = 0; index < resp.length; index++) {
selectedId = ids[index];
document.getElementById(ids[index]).onclick = (function() {
var currId = selectedId;
return function() {
prepareToEdit(currId);
}
})();
document.getElementById('delete-' + ids[index]).onclick = (function() {
var currId = selectedId;
return function() {
deleteTask(currId);
}
})();
}
} catch (e) {
return false;
}
}
});
}
function prepareToEdit(id) {
document.getElementById('input-id').value = id;
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function insertTask() {
newName = document.getElementById('input-name').value;
newDescription = document.getElementById('input-description').value;
if (newName == '' || newDescription == '') {
alert('Insert name and description');
return false;
}
newId = makeid();
$.ajax({
'type' : 'post',
'url' : window.location.href + 'methods/post_tasks.php',
'data' : {
name : newName,
description : newDescription,
id : newId
},
'success' : function(resp){
try {
alert('Successful insert new task');
} catch (e) {
return false;
}
}
});
getTasks();
}
function updateTask(id) {
newName = document.getElementById('input-name').value;
newDescription = document.getElementById('input-description').value;
id = document.getElementById('input-id').value;
if (id == undefined || id == '') {
alert('Select a task');
return false;
}
$.ajax({
'type' : 'post',
'url' : window.location.href + 'methods/update_tasks.php',
'data' : {
name : newName,
description : newDescription,
id : id
},
'success' : function(resp){
try {
alert('Successful update task');
} catch (e) {
return false;
}
}
});
getTasks();
}
function deleteTask(id) {
$.ajax({
'type' : 'post',
'url' : window.location.href + 'methods/delete_tasks.php',
'data' : {
id : id
},
'success' : function(resp){
try {
alert('Successful delete task');
} catch (e) {
return false;
}
}
});
getTasks();
}
</script>
</html>