回答編集履歴

2

追記

2016/11/18 18:52

投稿

退会済みユーザー
test CHANGED
@@ -233,3 +233,235 @@
233
233
  var_dump($res);
234
234
 
235
235
  ```
236
+
237
+
238
+
239
+ #追記 No2
240
+
241
+
242
+
243
+ ```php
244
+
245
+ <?php
246
+
247
+ ini_set('display_errors', true);
248
+
249
+ error_reporting(E_ALL);
250
+
251
+
252
+
253
+ /**
254
+
255
+ * index.php
256
+
257
+ */
258
+
259
+ require 'common.php';
260
+
261
+
262
+
263
+ $page = (int) filter_input(INPUT_GET, 'page') * 5;
264
+
265
+ $sql = sprintf('select * from User limit %d, 5', $page);
266
+
267
+ $users = Db::select($sql);
268
+
269
+
270
+
271
+ $sql_total = 'select * from User';
272
+
273
+ $total_rows = Db::select($sql_total);
274
+
275
+ $total = count($total_rows);
276
+
277
+ ?>
278
+
279
+ <!DOCTYPE HTML>
280
+
281
+ <html lang="ja">
282
+
283
+ <head>
284
+
285
+ <meta charset="UTF-8">
286
+
287
+ <title></title>
288
+
289
+ <style type="text/css">
290
+
291
+ #response {
292
+
293
+ height: 200px;
294
+
295
+ overflow: scroll;
296
+
297
+ background: #F6F6F6;
298
+
299
+ }
300
+
301
+ </style>
302
+
303
+ </head>
304
+
305
+ <body>
306
+
307
+ <div>
308
+
309
+ <div>
310
+
311
+ <button id='send' type="button">送信</button>
312
+
313
+ </div>
314
+
315
+ <table>
316
+
317
+ <thead>
318
+
319
+ <tr>
320
+
321
+ <th>
322
+
323
+ <label>
324
+
325
+ <input type="checkbox" id='checkall'>
326
+
327
+ 全選択
328
+
329
+ </label>
330
+
331
+ </th>
332
+
333
+ <th>id</th>
334
+
335
+ <th>name</th>
336
+
337
+ <th>email</th>
338
+
339
+ </tr>
340
+
341
+ </thead>
342
+
343
+ <tbody>
344
+
345
+ <?php foreach ($users as $user) : ?>
346
+
347
+ <tr>
348
+
349
+ <td>
350
+
351
+ <?php if (isset($_SESSION['user'][$user['id']]) && $_SESSION['user'][$user['id']]) : ?>
352
+
353
+ <input type="checkbox" class="user_chk" name='user[]' value="<?= h($user['id']); ?>" checked="checked" />
354
+
355
+ <?php else: ?>
356
+
357
+ <input type="checkbox" class="user_chk" name='user[]' value="<?= h($user['id']); ?>" />
358
+
359
+ <?php endif; ?>
360
+
361
+ </td>
362
+
363
+ <td><?= h($user['id']); ?></td>
364
+
365
+ <td><?= h($user['name']); ?></td>
366
+
367
+ <td><?= h($user['email']); ?></td>
368
+
369
+ </tr>
370
+
371
+ <?php endforeach; ?>
372
+
373
+ </tbody>
374
+
375
+ </table>
376
+
377
+ <?php echo pagination($total); ?>
378
+
379
+
380
+
381
+ <h3>Ajax Response</h3>
382
+
383
+ <div id="response">
384
+
385
+
386
+
387
+ </div>
388
+
389
+ </div>
390
+
391
+ <script type="text/javascript" src="//code.jquery.com/jquery-3.1.1.min.js"></script>
392
+
393
+ <script type="text/javascript">
394
+
395
+ $('.user_chk').on('change', function () {
396
+
397
+ var userid = $(this).val();
398
+
399
+ var checked = $(this).prop('checked');
400
+
401
+ $.ajax({
402
+
403
+ url: 'session.php'
404
+
405
+ , method: 'post'
406
+
407
+ , data: {
408
+
409
+ userid: userid
410
+
411
+ , value: checked
412
+
413
+ }
414
+
415
+ , success: function (response) {
416
+
417
+ var pre = $('<pre>');
418
+
419
+ pre.text(response);
420
+
421
+ $('#response').append(pre);
422
+
423
+ }
424
+
425
+ });
426
+
427
+ });
428
+
429
+
430
+
431
+ $("#checkall").on('click', function () {
432
+
433
+ $('.user_chk')
434
+
435
+ .prop('checked', $("#checkall").prop('checked'))
436
+
437
+ .trigger('change');
438
+
439
+ });
440
+
441
+
442
+
443
+ $("#send").on('click', function () {
444
+
445
+ $.ajax({
446
+
447
+ url: 'send.php'
448
+
449
+ , method: 'post'
450
+
451
+ , success: function (res) {
452
+
453
+ $('#response').text(res);
454
+
455
+ }
456
+
457
+ });
458
+
459
+ });
460
+
461
+ </script>
462
+
463
+ </body>
464
+
465
+ </html>
466
+
467
+ ```

1

追記

2016/11/18 18:52

投稿

退会済みユーザー
test CHANGED
@@ -91,3 +91,145 @@
91
91
  var_dump($_POST);
92
92
 
93
93
  ```
94
+
95
+ ---
96
+
97
+ #追記
98
+
99
+
100
+
101
+ common.php
102
+
103
+
104
+
105
+ ```php
106
+
107
+ <?php
108
+
109
+ $checkboxes = array(
110
+
111
+ 1 => 'チェック1'
112
+
113
+ , 2 => 'チェック1'
114
+
115
+ , 3 => 'チェック1'
116
+
117
+ , 4 => 'チェック1'
118
+
119
+ );
120
+
121
+ ```
122
+
123
+
124
+
125
+ index.php
126
+
127
+
128
+
129
+ ```php
130
+
131
+ <?php
132
+
133
+ require_once 'common.php';
134
+
135
+ ?><!DOCTYPE HTML>
136
+
137
+ <html lang="ja">
138
+
139
+ <head>
140
+
141
+ <meta charset="UTF-8">
142
+
143
+ <title></title>
144
+
145
+ </head>
146
+
147
+ <body>
148
+
149
+ <form action="">
150
+
151
+ <input type='hidden' name='issueid' value=10>
152
+
153
+ <?php foreach ($checkboxes as $val => $check): ?>
154
+
155
+ <input type="checkbox" name="user[]" value="<?= $val; ?>" /><?= $check; ?>
156
+
157
+ <?php endforeach; ?>
158
+
159
+ <button type="button" id="send">send</button>
160
+
161
+ </form>
162
+
163
+ <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
164
+
165
+ <script type="text/javascript">
166
+
167
+ $(function () {
168
+
169
+ $('#send').on('click', function () {
170
+
171
+ var data = $("form").serialize();
172
+
173
+
174
+
175
+ $.ajax({
176
+
177
+ method: 'post'
178
+
179
+ , data: data
180
+
181
+ , url: 'ajax.php'
182
+
183
+ , success: function (res) {
184
+
185
+ console.log(res);
186
+
187
+ }
188
+
189
+ });
190
+
191
+ });
192
+
193
+ });
194
+
195
+ </script>
196
+
197
+ </body>
198
+
199
+ </html>
200
+
201
+ ```
202
+
203
+
204
+
205
+ ajax.php
206
+
207
+
208
+
209
+ ```php
210
+
211
+ <?php
212
+
213
+
214
+
215
+ require_once 'common.php';
216
+
217
+
218
+
219
+ $post = filter_input_array(INPUT_POST);
220
+
221
+
222
+
223
+ $res = [];
224
+
225
+ foreach ($checkboxes as $val => $check) {
226
+
227
+ $res['user'][$val] = in_array($val, $post['user']);
228
+
229
+ }
230
+
231
+
232
+
233
+ var_dump($res);
234
+
235
+ ```