teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2016/11/18 18:52

投稿

退会済みユーザー
answer CHANGED
@@ -115,4 +115,120 @@
115
115
  }
116
116
 
117
117
  var_dump($res);
118
+ ```
119
+
120
+ #追記 No2
121
+
122
+ ```php
123
+ <?php
124
+ ini_set('display_errors', true);
125
+ error_reporting(E_ALL);
126
+
127
+ /**
128
+ * index.php
129
+ */
130
+ require 'common.php';
131
+
132
+ $page = (int) filter_input(INPUT_GET, 'page') * 5;
133
+ $sql = sprintf('select * from User limit %d, 5', $page);
134
+ $users = Db::select($sql);
135
+
136
+ $sql_total = 'select * from User';
137
+ $total_rows = Db::select($sql_total);
138
+ $total = count($total_rows);
139
+ ?>
140
+ <!DOCTYPE HTML>
141
+ <html lang="ja">
142
+ <head>
143
+ <meta charset="UTF-8">
144
+ <title></title>
145
+ <style type="text/css">
146
+ #response {
147
+ height: 200px;
148
+ overflow: scroll;
149
+ background: #F6F6F6;
150
+ }
151
+ </style>
152
+ </head>
153
+ <body>
154
+ <div>
155
+ <div>
156
+ <button id='send' type="button">送信</button>
157
+ </div>
158
+ <table>
159
+ <thead>
160
+ <tr>
161
+ <th>
162
+ <label>
163
+ <input type="checkbox" id='checkall'>
164
+ 全選択
165
+ </label>
166
+ </th>
167
+ <th>id</th>
168
+ <th>name</th>
169
+ <th>email</th>
170
+ </tr>
171
+ </thead>
172
+ <tbody>
173
+ <?php foreach ($users as $user) : ?>
174
+ <tr>
175
+ <td>
176
+ <?php if (isset($_SESSION['user'][$user['id']]) && $_SESSION['user'][$user['id']]) : ?>
177
+ <input type="checkbox" class="user_chk" name='user[]' value="<?= h($user['id']); ?>" checked="checked" />
178
+ <?php else: ?>
179
+ <input type="checkbox" class="user_chk" name='user[]' value="<?= h($user['id']); ?>" />
180
+ <?php endif; ?>
181
+ </td>
182
+ <td><?= h($user['id']); ?></td>
183
+ <td><?= h($user['name']); ?></td>
184
+ <td><?= h($user['email']); ?></td>
185
+ </tr>
186
+ <?php endforeach; ?>
187
+ </tbody>
188
+ </table>
189
+ <?php echo pagination($total); ?>
190
+
191
+ <h3>Ajax Response</h3>
192
+ <div id="response">
193
+
194
+ </div>
195
+ </div>
196
+ <script type="text/javascript" src="//code.jquery.com/jquery-3.1.1.min.js"></script>
197
+ <script type="text/javascript">
198
+ $('.user_chk').on('change', function () {
199
+ var userid = $(this).val();
200
+ var checked = $(this).prop('checked');
201
+ $.ajax({
202
+ url: 'session.php'
203
+ , method: 'post'
204
+ , data: {
205
+ userid: userid
206
+ , value: checked
207
+ }
208
+ , success: function (response) {
209
+ var pre = $('<pre>');
210
+ pre.text(response);
211
+ $('#response').append(pre);
212
+ }
213
+ });
214
+ });
215
+
216
+ $("#checkall").on('click', function () {
217
+ $('.user_chk')
218
+ .prop('checked', $("#checkall").prop('checked'))
219
+ .trigger('change');
220
+ });
221
+
222
+ $("#send").on('click', function () {
223
+ $.ajax({
224
+ url: 'send.php'
225
+ , method: 'post'
226
+ , success: function (res) {
227
+ $('#response').text(res);
228
+ }
229
+ });
230
+ });
231
+ </script>
232
+ </body>
233
+ </html>
118
234
  ```

1

追記

2016/11/18 18:52

投稿

退会済みユーザー
answer CHANGED
@@ -44,4 +44,75 @@
44
44
  <?php
45
45
 
46
46
  var_dump($_POST);
47
+ ```
48
+ ---
49
+ #追記
50
+
51
+ common.php
52
+
53
+ ```php
54
+ <?php
55
+ $checkboxes = array(
56
+ 1 => 'チェック1'
57
+ , 2 => 'チェック1'
58
+ , 3 => 'チェック1'
59
+ , 4 => 'チェック1'
60
+ );
61
+ ```
62
+
63
+ index.php
64
+
65
+ ```php
66
+ <?php
67
+ require_once 'common.php';
68
+ ?><!DOCTYPE HTML>
69
+ <html lang="ja">
70
+ <head>
71
+ <meta charset="UTF-8">
72
+ <title></title>
73
+ </head>
74
+ <body>
75
+ <form action="">
76
+ <input type='hidden' name='issueid' value=10>
77
+ <?php foreach ($checkboxes as $val => $check): ?>
78
+ <input type="checkbox" name="user[]" value="<?= $val; ?>" /><?= $check; ?>
79
+ <?php endforeach; ?>
80
+ <button type="button" id="send">send</button>
81
+ </form>
82
+ <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
83
+ <script type="text/javascript">
84
+ $(function () {
85
+ $('#send').on('click', function () {
86
+ var data = $("form").serialize();
87
+
88
+ $.ajax({
89
+ method: 'post'
90
+ , data: data
91
+ , url: 'ajax.php'
92
+ , success: function (res) {
93
+ console.log(res);
94
+ }
95
+ });
96
+ });
97
+ });
98
+ </script>
99
+ </body>
100
+ </html>
101
+ ```
102
+
103
+ ajax.php
104
+
105
+ ```php
106
+ <?php
107
+
108
+ require_once 'common.php';
109
+
110
+ $post = filter_input_array(INPUT_POST);
111
+
112
+ $res = [];
113
+ foreach ($checkboxes as $val => $check) {
114
+ $res['user'][$val] = in_array($val, $post['user']);
115
+ }
116
+
117
+ var_dump($res);
47
118
  ```