-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_postsample.html
126 lines (112 loc) · 3.52 KB
/
ajax_postsample.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
<html>
<head>
<title>js object tips</title>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
</head>
<body>
<?php
// echo('aaa');
$db = new SQLite3( 'sample.sqlite3' );
//var_dump($db);
$sql = 'SELECT * FROM user ORDER BY sort_id ASC';
$result = $db->query($sql);
// $db->close();
// var_dump($update);
$i = 1;
echo '<ul id="user" class="sortable">';
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo '<li class="list_' . $i++ . '">';
echo '<span class="user_id">'.$row['id'].'</span>';
echo '<input name="sort_id" class="sort_id" type="text" size="4" value="' . $row['sort_id'] .'" />';
echo $row['name'];
echo '<input name="age" class="user_age" type="text" size="4" value="' . $row['age'] .'" />';
//echo $row['age'];
echo '</li>';
}
echo '</ul>';
?>
<form>
<div>
<label for="name">名前</label><br />
<input id="name" name="name" type="text" size="50" />
</div>
<div>
<input id="btn" type="button" name="submit" value="send">
</div>
</form>
<ul id="result"></ul>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('btn').addEventListener('click', function() {
// document.getElementById('name');
var result = document.getElementById('result');
var xhr = new XMLHttpRequest();
console.log(xhr.readyState);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4){
if (xhr.status === 200) {
result.textContent = xhr.responseText;
} else {
result.textContent = 'server error';
}
} else {
result.textContent = '通信中';
}
};
xhr.open('POST', './ajax_sample.php', true);
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
xhr.send('name=' + encodeURIComponent(document.getElementById('name').value));
}, false);
// [].forEach.call(document.getElementsByClassName("sort_id"),function(x){
// x.addEventListener('change', function() {
// console.log('sss');
// });
// });
}, false);
$(function() {
$('input').change(function() {
var age = $(this).val();
var user_id = $( this ).parent( 'li' ).children('span.user_id').text();
var class_name = $( this ).attr( 'class' );
// console.log($(this).val());
// console.log( $( this ).attr( 'class' ) );
// console.log( $( this ).parent( 'li' ).children('span.user_id').text() );
if(class_name == 'user_age'){
$.ajax({
type: "POST",
url: "ajax_post.php",
data: "id=" + user_id + "&age=" + age,
success: function(msg){
alert( "Saved: " + msg );
}
});
}
});
$(".sortable").sortable({
update: function() {
$('ul li').each(function(i){
$(this).attr('id','element' + (i+1));
$(this).children('.sort_id').val(i+1);
var user_id = $( this ).children('span.user_id').text();
var sort_id = i+1;
$.ajax({
type: "POST",
url: "update_sort_id.php",
data: "id=" + user_id + "&sort_id=" + sort_id,
success: function(msg) {
alert('success: ' + msg);
}
});
console.log('change');
});
}
});
$(".sortable").disableSelection();
});
</script>
</body>
</html>