【问题标题】:Send Data From FORM JSON; using async fetch await to PHP MySQL从 FORM JSON 发送数据;对 PHP MySQL 使用 async fetch await
【发布时间】:2020-10-08 22:05:20
【问题描述】:

几天以来,我尝试解决此问题。 需要什么: 1) 将数据从 JSON (async fetch await) 发布到 php 2) 接收数据并将其上传到我的服务器上

实际上,从现在开始,我从我的 PHP 中收到了一个答案,但这个答案在我的 console.log 中是空的。

请看下面的代码:

从 FORM JSON 发送数据;使用 async fetch await 到 PHP MySQL

感谢您的帮助,像往常一样,我会继续寻找答案。这个会发。

表格

  <form id="form">
    <div id="areachatbox"></div>
    <textarea type="text" id="message" name="message" ></textarea>
    <input id="submit" type="submit" value="Send">
  </form>

JSON:

        <script>
const form = document.getElementById('form');

 form.addEventListener('click', textarea);

     async function textarea(event) {
      event.preventDefault();

      const msg = document.getElementById('message').value;



      const response = await fetch('chatpost.php', {
        method: 'post',
        body:JSON.stringify({msg})
      })

      const data = await response.text();
      console.log(data);
    }

    </script>

和 PHP

<?php 

$json = json_decode(file_get_contents('http://localhost/XXXXXX/homepage.php'), true);

echo $json['msg'];

?> 

【问题讨论】:

    标签: php mysql arrays json async-await


    【解决方案1】:

    试试这个并告诉我:

    $json=json_decode(file_get_contents('php://input'),true);
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 2015-12-25
      • 2019-01-29
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      相关资源
      最近更新 更多