0

I'm trying to send some data to a php script and then return some other data. I can't figure out how to access the data I send to the php script. This post on stackoverflow was not helpful: Reading JSON POST using PHP Is the sent data in $_POST? Is it in $data or $params? How can I access it and print it out.

Here's what I have:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Http Requests & JavaScript</title>
  <link rel="stylesheet" href="app.css">
  <script src="xhr.js" defer></script>
</head>
<body>
  <section id="control-center">
    <button id="get-btn">GET Data</button>
    <button id="post-btn">POST Data</button>
  </section>
</body>
</html>

xhr.js

const getBtn = document.getElementById('get-btn');
const postBtn = document.getElementById('post-btn');




const sendHttpRequest = (method, url, data) => {
    const promise = new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open(method, url);
        xhr.responseType = 'json';
        if (data){
            xhr.setRequestHeader('Content-Type', 'application/json');
        }

        xhr.onload = () => {
            resolve(xhr.response);
        }
        xhr.send(JSON.stringify(data));
    });
    console.log(promise);
    return promise;
};


const getData = () => {
    sendHttpRequest('GET', 'http://localhost/async/data.json').then(responseData => {
        console.log(response);

    });
};

const sendData = () => {
    sendHttpRequest('POST', 'http://localhost/async/data.php', {
        email: 'drcrocket@gmail.com',
        password: 'bowie'
    }).then(responseData => {
        console.log(responseData);
    });
};

getBtn.addEventListener('click', getData);
postBtn.addEventListener('click', sendData);

data.php

<?php

$myJson = '{
       "per_page":"6",
       "total":"12",
       "total_pages":"2",
       "data":
           [
               {
                   "id":"1",
                   "email":"rdumdumle@gmail.com",
                   "first_name":"david",
                   "last_name":"escabar"
               },
               {
                   "id":"2",
                   "email":"bluesky@gmail.com",
                   "first_name":"Willie",
                   "last_name":"Nelson"
               }
           ]
   }';


   print_r($myJson);
   //print_r($data);

   //print_r($_POST);





 ?>
DCR
  • 14,737
  • 12
  • 52
  • 115
  • The answer is in the question you linked to: `$json = file_get_contents('php://input'); $obj = json_decode($json);` – Barmar May 26 '21 at 22:59
  • Then you use `$obj->email` and `$obj->password` to access the parameters. – Barmar May 26 '21 at 23:01
  • @Barmar, thank you. I noticed in the docs php://input is not available with enctype="multipart/form-data".. How do you read sent data in that case? – DCR May 26 '21 at 23:09
  • There's two ways to read inbound data, 1) php://input (usually when Content-Type is `application/json`) or 2) `$_POST`. Depending on your php version there is also $HTTP_RAW_POST_DATA but that is deprecated and shouldn't be used. – zanderwar May 26 '21 at 23:16
  • You don't need it. When you use multipart/form-data, you can get the parameters from `$_POST` and `$_FILES`. – Barmar May 26 '21 at 23:20
  • You only need to use `php://input` when the contents are not `application/x-www-form-urlencoded` or `multipart/form-data`, since PHP parses those automatically. – Barmar May 26 '21 at 23:21

0 Answers0