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

質問編集履歴

2

受け取り側ファイルを修正

2021/04/29 09:06

投稿

kazumad
kazumad

スコア9

title CHANGED
File without changes
body CHANGED
@@ -28,47 +28,152 @@
28
28
  </head>
29
29
 
30
30
  <body>
31
- <h1>チャット参加メンバー、一覧機能のテスト</h1>
32
- <ul>
33
- <li></li>
31
+ <h1>チャット</h1>
32
+ <p id="user">現在入室しているユーザー 一覧</p>
33
+ <!--この下にユーザー名を追加していく-->
34
- <li></li>
34
+ <ul id="list">
35
- <li></li>
35
+ <!-- liをここに追加-->
36
36
  </ul>
37
-
38
- 名前
37
+ <p>名前</p>
39
38
  <div><input id="name" type="text" name=""></div>
40
- メッセージ
39
+ <p>メッセージ</p>
41
40
  <div><textarea id="chat_submit" type="text" name=""></textarea></div>
42
41
 
43
42
  <button type="button" id="test_btn">test</button>
44
43
  <script>
45
- //
46
44
  $(function() {
47
- //select API
45
+ //insert API
46
+ //入室したらランダムな名前を割り振り情報(name,id)がdbに追加される
47
+ //まずランダム名を割り振るコード
48
+ let random_name = Math.random().toString(32).substring(2) ;
49
+ console.log(random_name);
50
+ let name_data = {'random_name':random_name};
51
+ console.log(name_data);
52
+
53
+
54
+ //let tuika = random_name;
55
+ //変数 連結
56
+ //$test = "<p>{$hensu}</p>";
57
+ // $test = '<p>' . $hensu . '</p>';
58
+ //let test = `<p>${hensu}</p>`;
59
+ // let test = '<p>' + hensu + '</p>';
60
+ //$("#user").append(`<p>${tuika}</p>`);
61
+
62
+
63
+ //dbにランダム名を送る
48
64
  $.ajax({
49
- url:'http://localhost:8888/api.php',
65
+ url:'http://localhost:8888/api_user.php',
50
66
  type: "post",
51
67
  //data 送信するデータ(「キー名: 値」のハッシュ形式)
52
68
  dataType:'json',
69
+ data:name_data,
53
70
  async: true
54
71
  })
72
+ .done(function (data) {
73
+ if(data){
74
+ console.log(data);
75
+ console.log('ユーザー成功');
76
+ }
77
+ // 通信成功時の処理を記述
78
+ console.log(data);
79
+ })
80
+ .fail(function () {
81
+ // 通信失敗時の処理を記述
82
+ //console.log('');
83
+ });
84
+ //room_member API
85
+ setInterval(function () {
86
+ $.ajax({
87
+ url: 'http://localhost:8888/room_member.php',
88
+ type: "get",
89
+ dataType:'json',
90
+ //data 送信するデータ(「キー名: 値」のハッシュ形式)
91
+ async: true
92
+ })
93
+ .done(function (data) {
94
+ // 通信成功時の処理を記述
95
+ console.log(data);
96
+ console.log('一覧成功');
55
97
 
98
+ //$("ul").append(`<li>${data.name}</li>`);
56
99
 
100
+ $('ul').html('');
101
+
102
+ $.each(data, function(index, element) {
103
+ $('ul').append(index + ':' + element.name + '<br>');
104
+ });
105
+ //人数分回す
106
+ // $('#list').each(function() {
107
+ // //繰り返し処理を記述する
108
+ // $("#list").append(`<li>${data.name}</li>`);
109
+ // })
110
+ })
111
+ .fail(function () {
112
+ // 通信失敗時の処理を記述
113
+ console.log('失敗');
114
+ });
115
+ }, 5000);
116
+ //delete API
117
+ //ウインドウ閉じたらユーザ削除
118
+ $(window).on('beforeunload', function() {
119
+ $.ajax({
120
+ url:'http://localhost:8888/api_delete.php',
121
+ type: "post",
122
+ //data 送信するデータ(「キー名: 値」のハッシュ形式)
123
+ dataType:"json",
124
+ data:name_data,
125
+ async: true
126
+ })
57
127
  .done(function (data) {
58
-
128
+ if(data){
59
129
  console.log(data);
60
130
  console.log('ユーザー成功');
131
+ }
132
+ // 通信成功時の処理を記述
133
+ console.log(data);
134
+ })
135
+ .fail(function () {
136
+ // 通信失敗時の処理を記述
137
+ //console.log('');
138
+ });
139
+ });
61
140
 
141
+
142
+ //select API
143
+ //submitイベントを使い、フォーム(js-from)が送信された時に処理が実行されるようにイベントを設定。
144
+ $('#test_btn').on('click', function(){
145
+ let name = $('#name').val();
146
+ let chat_submit = $('#chat_submit').val();
147
+ console.log(name);
148
+ console.log(chat_submit);
149
+ let data = { 'name':name,
150
+ 'chat_submit':chat_submit
151
+ };
152
+ console.log(data);
153
+ $.ajax({
154
+ url:'http://localhost:8888/api.php',
155
+ type: "get",
156
+ dataType:'json',
157
+ //data 送信するデータ(「キー名: 値」のハッシュ形式)
158
+ data:data,
159
+ async: true
160
+ })
161
+ .done(function (data) {
162
+ if(data){
163
+ console.log('成功');
164
+
165
+ }
62
166
  // 通信成功時の処理を記述
63
167
  console.log(data);
64
168
  })
65
169
  .fail(function () {
66
170
  // 通信失敗時の処理を記述
67
- console.log('失敗');
171
+ //console.log('');
68
172
  });
69
-
173
+ });
70
174
  });
71
175
  </script>
72
176
  </body>
73
177
  </html>
178
+
74
179
  ```

1

受け取り側のファイルhtmlファイル  中身(html タグ jquery) を追記しました

2021/04/29 09:06

投稿

kazumad
kazumad

スコア9

title CHANGED
File without changes
body CHANGED
@@ -16,4 +16,59 @@
16
16
  header("Content-type: application/json; charset=UTF-8");
17
17
  //JSONデータを出力
18
18
  echo json_encode($row_1->fetch_assoc());
19
+ ```
20
+ 受け取り側のファイルを追記します
21
+ ```html
22
+ <!DOCTYPE html>
23
+ <html lang="ja">
24
+ <head>
25
+ <meta charset="utf-8">
26
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
27
+ <title>チャット</title>
28
+ </head>
29
+
30
+ <body>
31
+ <h1>チャット参加メンバー、一覧機能のテスト</h1>
32
+ <ul>
33
+ <li></li>
34
+ <li></li>
35
+ <li></li>
36
+ </ul>
37
+
38
+ 名前
39
+ <div><input id="name" type="text" name=""></div>
40
+ メッセージ
41
+ <div><textarea id="chat_submit" type="text" name=""></textarea></div>
42
+
43
+ <button type="button" id="test_btn">test</button>
44
+ <script>
45
+ //
46
+ $(function() {
47
+ //select API
48
+ $.ajax({
49
+ url:'http://localhost:8888/api.php',
50
+ type: "post",
51
+ //data 送信するデータ(「キー名: 値」のハッシュ形式)
52
+ dataType:'json',
53
+ async: true
54
+ })
55
+
56
+
57
+ .done(function (data) {
58
+
59
+ console.log(data);
60
+ console.log('ユーザー成功');
61
+
62
+ // 通信成功時の処理を記述
63
+ console.log(data);
64
+ })
65
+ .fail(function () {
66
+ // 通信失敗時の処理を記述
67
+ console.log('失敗');
68
+ });
69
+
70
+ });
71
+ </script>
72
+ </body>
73
+ </html>
19
74
  ```