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

質問編集履歴

2

追記

2021/11/30 14:30

投稿

shos3
shos3

スコア15

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,10 @@
8
8
 
9
9
 
10
10
  ・削除ボタンでアラート表示OKなら削除
11
+
12
+
13
+
11
- ```php
14
+ ```php.
12
15
  <?php
13
16
 
14
17
  $sql = 'select id, name, kana, tel, email, body, created_at from contacts';
@@ -32,92 +35,65 @@
32
35
  echo "\t\t<td> </td>\n";
33
36
  echo "\t\t<td>{$result['created_at']}</td>\n";
34
37
  echo "\t\t<td> </td>\n";
35
- echo "\t\t<td><a href=edit.php id=".$result['id'].">編集</a>"."</td>\n";
38
+ echo "\t\t<td><a href=edit.php? id=".$result['id'].">編集</a>"."</td>\n";
36
39
  echo "\t\t<td> </td>\n";
37
40
  ?>
38
- <td><a href=contact.php <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td</td>
41
+ <td><a href= contact.php class="deleteBtn" data-id='<?php echo $result['id'] ;?>' onClick="return dspDelete();" >削除</a></td>
39
42
  <?php
40
43
  echo "\t</tr>\n";
41
- ?>
44
+ ?>
42
-
43
- <script>
44
- function dispDelete(){
45
- if(!window.confirm('削除しますか?')){
46
- window.alert('キャンセルされました');
47
- return false;
48
- }
49
- return true;{
50
- <?php if(!empty($result['id'])) {
51
- $stmt = $db->prepare('DELETE FROM contacts WHERE id = :id');
52
- $stmt->execute(array(':id' => $result["id"]));
53
- $stmt->execute(); ?>
54
- }
55
- }
56
45
 
57
- </script>
58
-
59
- <?php echo "</table>\n";
60
-
61
- }
62
- }
63
- ?>
64
-
65
- ```
66
- 変更後
67
- ```php.
68
- echo "<table>\n";
69
- 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";
70
- while( $result = $stmt->fetch( PDO::FETCH_ASSOC ) ){
71
- echo "\t<tr>\n";
72
- echo "\t\t<td>{$result['id']}</td>\n";
73
- echo "\t\t<td> </td>\n";
74
- echo "\t\t<td>{$result['name']}</td>\n";
75
- echo "\t\t<td> </td>\n";
76
- echo "\t\t<td>{$result['kana']}</td>\n";
77
- echo "\t\t<td> </td>\n";
78
- echo "\t\t<td>{$result['tel']}</td>\n";
79
- echo "\t\t<td> </td>\n";
80
- echo "\t\t<td>{$result['email']}</td>\n";
81
- echo "\t\t<td> </td>\n";
82
- echo "\t\t<td>{$result['body']}</td>\n";
83
- echo "\t\t<td> </td>\n";
84
- echo "\t\t<td>{$result['created_at']}</td>\n";
85
- echo "\t\t<td> </td>\n";
86
- echo "\t\t<td><a href=edit.php? id=".$result['id'].">編集</a>"."</td>\n";
87
- echo "\t\t<td> </td>\n";
88
- ?>
89
- <td><a href=contact.php? class="deleteBtn" <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td>
90
- <?php
91
- echo "\t</tr>\n";
92
-
93
- echo "</table>\n";
94
- ?>
95
-
96
- <script>
46
+ <script>
97
47
  $(".deleteBtn").click(function(){
48
+
98
- var btnid = $(this).data($result['id']);
49
+ var btnid = $(this).data("id");
50
+ console.log(btnid);
99
51
  deleteData(btnid);
100
- });
52
+ });
53
+
54
+
55
+
101
56
  function deleteData(btnid){
57
+ if(window.confirm('削除しますか?')){
102
58
  $.ajax({
103
59
  type: 'POST',
104
60
  dataType:'json',
105
- url:'contact.php?',
61
+ url:'delete_func.php',
106
62
  data:{
107
63
  btnid:btnid,
108
64
  },
65
+
109
- success:function(data) {
66
+ success:function(data) {
67
+ console.log(data);
110
- window.location.href = "./";
68
+ window.location.href = "./";
111
- },
69
+ },
112
- error:function(XMLHttpRequest, textStatus, errorThrown) {
70
+ error:function(XMLHttpRequest, textStatus, errorThrown) {
113
- alert(errorThrown);
71
+ alert(errorThrown);
72
+
114
- }
73
+ }
74
+
115
75
  });
76
+
116
- };
77
+ }
78
+ }
117
79
  </script>
118
80
 
119
81
  <?php
120
82
 
121
- }
83
+ }
84
+ echo "</table>\n";
85
+
122
86
  ?>
87
+ ```
88
+
89
+ delete_func.php
90
+ ```
91
+
92
+ $jsondata = $_POST['btnid'];
93
+
94
+
95
+ $stmt = $db->prepare('DELETE FROM contacts WHERE id = '.$jsondata.' ');
96
+ $stmt->execute();
97
+ header('Content-type: application/json');
98
+ echo json_encode( $jsondata );
123
99
  ```

1

追記

2021/11/30 14:30

投稿

shos3
shos3

スコア15

title CHANGED
File without changes
body CHANGED
@@ -62,4 +62,62 @@
62
62
  }
63
63
  ?>
64
64
 
65
+ ```
66
+ 変更後
67
+ ```php.
68
+ echo "<table>\n";
69
+ 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";
70
+ while( $result = $stmt->fetch( PDO::FETCH_ASSOC ) ){
71
+ echo "\t<tr>\n";
72
+ echo "\t\t<td>{$result['id']}</td>\n";
73
+ echo "\t\t<td> </td>\n";
74
+ echo "\t\t<td>{$result['name']}</td>\n";
75
+ echo "\t\t<td> </td>\n";
76
+ echo "\t\t<td>{$result['kana']}</td>\n";
77
+ echo "\t\t<td> </td>\n";
78
+ echo "\t\t<td>{$result['tel']}</td>\n";
79
+ echo "\t\t<td> </td>\n";
80
+ echo "\t\t<td>{$result['email']}</td>\n";
81
+ echo "\t\t<td> </td>\n";
82
+ echo "\t\t<td>{$result['body']}</td>\n";
83
+ echo "\t\t<td> </td>\n";
84
+ echo "\t\t<td>{$result['created_at']}</td>\n";
85
+ echo "\t\t<td> </td>\n";
86
+ echo "\t\t<td><a href=edit.php? id=".$result['id'].">編集</a>"."</td>\n";
87
+ echo "\t\t<td> </td>\n";
88
+ ?>
89
+ <td><a href=contact.php? class="deleteBtn" <?php echo $result['id'] ;?> onClick="return dispDelete();" >削除</a></td>
90
+ <?php
91
+ echo "\t</tr>\n";
92
+
93
+ echo "</table>\n";
94
+ ?>
95
+
96
+ <script>
97
+ $(".deleteBtn").click(function(){
98
+ var btnid = $(this).data($result['id']);
99
+ deleteData(btnid);
100
+ });
101
+ function deleteData(btnid){
102
+ $.ajax({
103
+ type: 'POST',
104
+ dataType:'json',
105
+ url:'contact.php?',
106
+ data:{
107
+ btnid:btnid,
108
+ },
109
+ success:function(data) {
110
+ window.location.href = "./";
111
+ },
112
+ error:function(XMLHttpRequest, textStatus, errorThrown) {
113
+ alert(errorThrown);
114
+ }
115
+ });
116
+ };
117
+ </script>
118
+
119
+ <?php
120
+
121
+ }
122
+ ?>
65
123
  ```