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

回答編集履歴

1

tuika

2019/11/14 00:27

投稿

yambejp
yambejp

スコア118400

answer CHANGED
@@ -17,4 +17,29 @@
17
17
  });
18
18
  </script>
19
19
  <div contenteditable="true">test</div>
20
+ ```
21
+
22
+ # ajaxでpost
23
+ ajaxはfetchにシフトして、データはformdataで持ちます
24
+ postのタイミングがなにかのトリガーによる場合は前回回答した
25
+ MutationObserverも不要です
26
+ ```javascript
27
+ <script>
28
+ window.addEventListener('DOMContentLoaded', ()=>{
29
+ document.querySelector('#btn').addEventListener('click',()=>{
30
+ const method="POST";
31
+ const body=new FormData;
32
+ const url="hoge.php";
33
+ body.append("val",document.querySelector('[contenteditable]').textContent);
34
+ fetch(url,{method,body}).then(res=>res.text()).then(console.log);
35
+ });
36
+ });
37
+ </script>
38
+ <div contenteditable="true">test</div>
39
+ <input type="button" id="btn" value="send">
40
+ ```
41
+ - hoge.php
42
+ ```PHP
43
+ <?PHP
44
+ print_r($_POST);
20
45
  ```