-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripst.js
76 lines (76 loc) · 2.12 KB
/
scripst.js
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
$(document).ready(function(){
// save comment to database
$(document).on('click', '#submit_btn', function(){
var name = $('#name').val();
var comment = $('#comment').val();
$.ajax({
url: 'server.php',
type: 'POST',
data: {
'save': 1,
'name': name,
'comment': comment,
},
success: function(response){
$('#name').val('');
$('#comment').val('');
$('#display_area').append(response);
}
});
});
// delete from database
$(document).on('click', '.delete', function(){
var id = $(this).data('id');
$clicked_btn = $(this);
$.ajax({
url: 'server.php',
type: 'GET',
data: {
'delete': 1,
'id': id,
},
success: function(response){
// remove the deleted comment
$clicked_btn.parent().remove();
$('#name').val('');
$('#comment').val('');
}
});
});
var edit_id;
var $edit_comment;
$(document).on('click', '.edit', function(){
edit_id = $(this).data('id');
$edit_comment = $(this).parent();
// grab the comment to be editted
var name = $(this).siblings('.display_name').text();
var comment = $(this).siblings('.comment_text').text();
// place comment in form
$('#name').val(name);
$('#comment').val(comment);
$('#submit_btn').hide();
$('#update_btn').show();
});
$(document).on('click', '#update_btn', function(){
var id = edit_id;
var name = $('#name').val();
var comment = $('#comment').val();
$.ajax({
url: 'server.php',
type: 'POST',
data: {
'update': 1,
'id': id,
'name': name,
'comment': comment,
},
success: function(response){
$('#name').val('');
$('#comment').val('');
$('#submit_btn').show();
$('#update_btn').hide();
$edit_comment.replaceWith(response);
}
});
});
});