質問編集履歴

2

追記

2021/11/30 14:30

投稿

shos3
shos3

スコア15

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,13 @@
18
18
 
19
19
  ・削除ボタンでアラート表示OKなら削除
20
20
 
21
+
22
+
23
+
24
+
25
+
26
+
21
- ```php
27
+ ```php.
22
28
 
23
29
  <?php
24
30
 
@@ -66,139 +72,45 @@
66
72
 
67
73
  echo "\t\t<td> </td>\n";
68
74
 
69
- echo "\t\t<td><a href=edit.php id=".$result['id'].">編集</a>"."</td>\n";
70
-
71
- echo "\t\t<td> </td>\n";
72
-
73
- ?>
74
-
75
- <td><a href=contact.php <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td</td>
76
-
77
- <?php
78
-
79
- echo "\t</tr>\n";
80
-
81
- ?>
82
-
83
-
84
-
85
- <script>
86
-
87
- function dispDelete(){
88
-
89
- if(!window.confirm('削除しますか?')){
90
-
91
- window.alert('キャンセルされました');
92
-
93
- return false;
94
-
95
- }
96
-
97
- return true;{
98
-
99
- <?php if(!empty($result['id'])) {
100
-
101
- $stmt = $db->prepare('DELETE FROM contacts WHERE id = :id');
102
-
103
- $stmt->execute(array(':id' => $result["id"]));
104
-
105
- $stmt->execute(); ?>
106
-
107
- }
108
-
109
- }
110
-
111
-
112
-
113
- </script>
114
-
115
-
116
-
117
- <?php echo "</table>\n";
118
-
119
-
120
-
121
- }
122
-
123
- }
124
-
125
- ?>
126
-
127
-
128
-
129
- ```
130
-
131
- 変更後
132
-
133
- ```php.
134
-
135
- echo "<table>\n";
136
-
137
- echo "\t<tr><th>id</th><th></th><th>name</th><th></th><th>kana</th><th></th><th>tel</th><th></th><th>email</th><th></th><th>body</th><th></th><th>created_at</th></tr>\n";
138
-
139
- while( $result = $stmt->fetch( PDO::FETCH_ASSOC ) ){
140
-
141
- echo "\t<tr>\n";
142
-
143
- echo "\t\t<td>{$result['id']}</td>\n";
144
-
145
- echo "\t\t<td> </td>\n";
146
-
147
- echo "\t\t<td>{$result['name']}</td>\n";
148
-
149
- echo "\t\t<td> </td>\n";
150
-
151
- echo "\t\t<td>{$result['kana']}</td>\n";
152
-
153
- echo "\t\t<td> </td>\n";
154
-
155
- echo "\t\t<td>{$result['tel']}</td>\n";
156
-
157
- echo "\t\t<td> </td>\n";
158
-
159
- echo "\t\t<td>{$result['email']}</td>\n";
160
-
161
- echo "\t\t<td> </td>\n";
162
-
163
- echo "\t\t<td>{$result['body']}</td>\n";
164
-
165
- echo "\t\t<td> </td>\n";
166
-
167
- echo "\t\t<td>{$result['created_at']}</td>\n";
168
-
169
- echo "\t\t<td> </td>\n";
170
-
171
75
  echo "\t\t<td><a href=edit.php? id=".$result['id'].">編集</a>"."</td>\n";
172
76
 
173
77
  echo "\t\t<td> </td>\n";
174
78
 
175
79
  ?>
176
80
 
177
- <td><a href=contact.php? class="deleteBtn" <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td>
81
+ <td><a href= contact.php class="deleteBtn" data-id='<?php echo $result['id'] ;?>' onClick="return dspDelete();" >削除</a></td>
178
82
 
179
83
  <?php
180
84
 
181
85
  echo "\t</tr>\n";
182
86
 
87
+ ?>
183
88
 
89
+
184
90
 
185
- echo "</table>\n";
91
+ <script>
186
92
 
93
+ $(".deleteBtn").click(function(){
94
+
95
+
96
+
97
+ var btnid = $(this).data("id");
98
+
99
+ console.log(btnid);
100
+
101
+ deleteData(btnid);
102
+
187
- ?>
103
+ });
188
104
 
189
105
 
190
106
 
191
- <script>
107
+
192
108
 
193
- $(".deleteBtn").click(function(){
109
+
194
-
195
- var btnid = $(this).data($result['id']);
196
-
197
- deleteData(btnid);
198
-
199
- });
200
110
 
201
111
  function deleteData(btnid){
112
+
113
+ if(window.confirm('削除しますか?')){
202
114
 
203
115
  $.ajax({
204
116
 
@@ -206,7 +118,7 @@
206
118
 
207
119
  dataType:'json',
208
120
 
209
- url:'contact.php?',
121
+ url:'delete_func.php',
210
122
 
211
123
  data:{
212
124
 
@@ -214,21 +126,33 @@
214
126
 
215
127
  },
216
128
 
217
- success:function(data) {
129
+
218
130
 
219
- window.location.href = "./";
131
+ success:function(data) {
220
132
 
221
- },
133
+ console.log(data);
222
134
 
223
- error:function(XMLHttpRequest, textStatus, errorThrown) {
135
+ window.location.href = "./";
224
136
 
225
- alert(errorThrown);
137
+ },
226
138
 
139
+ error:function(XMLHttpRequest, textStatus, errorThrown) {
140
+
141
+ alert(errorThrown);
142
+
143
+
144
+
227
- }
145
+ }
146
+
147
+
228
148
 
229
149
  });
230
150
 
151
+
152
+
231
- };
153
+ }
154
+
155
+ }
232
156
 
233
157
  </script>
234
158
 
@@ -238,8 +162,36 @@
238
162
 
239
163
 
240
164
 
241
- }
165
+ }
166
+
167
+ echo "</table>\n";
168
+
169
+
242
170
 
243
171
  ?>
244
172
 
245
173
  ```
174
+
175
+
176
+
177
+ delete_func.php
178
+
179
+ ```
180
+
181
+
182
+
183
+ $jsondata = $_POST['btnid'];
184
+
185
+
186
+
187
+
188
+
189
+ $stmt = $db->prepare('DELETE FROM contacts WHERE id = '.$jsondata.' ');
190
+
191
+ $stmt->execute();
192
+
193
+ header('Content-type: application/json');
194
+
195
+ echo json_encode( $jsondata );
196
+
197
+ ```

1

追記

2021/11/30 14:30

投稿

shos3
shos3

スコア15

test CHANGED
File without changes
test CHANGED
@@ -127,3 +127,119 @@
127
127
 
128
128
 
129
129
  ```
130
+
131
+ 変更後
132
+
133
+ ```php.
134
+
135
+ echo "<table>\n";
136
+
137
+ echo "\t<tr><th>id</th><th></th><th>name</th><th></th><th>kana</th><th></th><th>tel</th><th></th><th>email</th><th></th><th>body</th><th></th><th>created_at</th></tr>\n";
138
+
139
+ while( $result = $stmt->fetch( PDO::FETCH_ASSOC ) ){
140
+
141
+ echo "\t<tr>\n";
142
+
143
+ echo "\t\t<td>{$result['id']}</td>\n";
144
+
145
+ echo "\t\t<td> </td>\n";
146
+
147
+ echo "\t\t<td>{$result['name']}</td>\n";
148
+
149
+ echo "\t\t<td> </td>\n";
150
+
151
+ echo "\t\t<td>{$result['kana']}</td>\n";
152
+
153
+ echo "\t\t<td> </td>\n";
154
+
155
+ echo "\t\t<td>{$result['tel']}</td>\n";
156
+
157
+ echo "\t\t<td> </td>\n";
158
+
159
+ echo "\t\t<td>{$result['email']}</td>\n";
160
+
161
+ echo "\t\t<td> </td>\n";
162
+
163
+ echo "\t\t<td>{$result['body']}</td>\n";
164
+
165
+ echo "\t\t<td> </td>\n";
166
+
167
+ echo "\t\t<td>{$result['created_at']}</td>\n";
168
+
169
+ echo "\t\t<td> </td>\n";
170
+
171
+ echo "\t\t<td><a href=edit.php? id=".$result['id'].">編集</a>"."</td>\n";
172
+
173
+ echo "\t\t<td> </td>\n";
174
+
175
+ ?>
176
+
177
+ <td><a href=contact.php? class="deleteBtn" <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td>
178
+
179
+ <?php
180
+
181
+ echo "\t</tr>\n";
182
+
183
+
184
+
185
+ echo "</table>\n";
186
+
187
+ ?>
188
+
189
+
190
+
191
+ <script>
192
+
193
+ $(".deleteBtn").click(function(){
194
+
195
+ var btnid = $(this).data($result['id']);
196
+
197
+ deleteData(btnid);
198
+
199
+ });
200
+
201
+ function deleteData(btnid){
202
+
203
+ $.ajax({
204
+
205
+ type: 'POST',
206
+
207
+ dataType:'json',
208
+
209
+ url:'contact.php?',
210
+
211
+ data:{
212
+
213
+ btnid:btnid,
214
+
215
+ },
216
+
217
+ success:function(data) {
218
+
219
+ window.location.href = "./";
220
+
221
+ },
222
+
223
+ error:function(XMLHttpRequest, textStatus, errorThrown) {
224
+
225
+ alert(errorThrown);
226
+
227
+ }
228
+
229
+ });
230
+
231
+ };
232
+
233
+ </script>
234
+
235
+
236
+
237
+ <?php
238
+
239
+
240
+
241
+ }
242
+
243
+ ?>
244
+
245
+ ```