2

I am posting to PHP file an OBJECT, the question is, I am not using name="" like when we do it inside the forms, but Instead, I am posting an Object, look:

    const done = async obj => {
    console.log("sending JSON to PHP...");
     await fetch("myFile.php",
        {
            method: "post",
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(obj)
        }
    )
}
let obj = {
    email: "ahmad@example.com",
    message: "salam"
}
const send = async (obj)=>{
    let response = await done(obj);
}
send(obj);

The question is, How the PHP will resolve the posted arguments?

here is what I've tried:

$email = $_POST['email'];
$message = $_POST['message'];

but as usual, this isn't working.

[EDIT]:

Extra question: is it important to send the POSTED data as JSON format? Is it important to do that while using the fetch API?

Mathew
  • 129
  • 9

2 Answers2

4

Try something like this

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

$email = $data['email'];
$message = $data['message'];
bumperbox
  • 10,166
  • 6
  • 43
  • 66
2

As you have used stringify, your php file will receive a string representation of your json object. So you will have to use json_decode to convert the string to a php object.

$json = file_get_contents('php://input');
$data = json_decode($json);
$email = $data->email;

Further clarification: If the request body contains string representation of JSON, content-type header will be text/plain, even if the Content-Type is set to application/json. So, in php file, you will have to read the raw text in body with file_get_content first as string and the convert it to php json object.

smartdroid
  • 312
  • 2
  • 10
  • I tried to do: $email = json_decode($_POST['email']); $message = json_decode($_POST['message']); but it didn't work. – Mathew Jun 28 '20 at 23:15
  • what should I do? – Mathew Jun 28 '20 at 23:16
  • I am getting this error – Mathew Jun 28 '20 at 23:16
  • Notice: Undefined index: email in /opt/lampp/htdocs/file.php on line 8 Notice: Undefined index: message in /opt/lampp/htdocs/file.php on line 9 – Mathew Jun 28 '20 at 23:16
  • I have added code in answer. – smartdroid Jun 28 '20 at 23:21
  • Thanks for help, could you please explain the first line a little bit? – Mathew Jun 28 '20 at 23:22
  • 1
    The first line gets the raw data from request body. – smartdroid Jun 28 '20 at 23:23
  • 1
    See reference here on how to set the request body https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#Parameters – smartdroid Jun 28 '20 at 23:25
  • @mathew if this resolves your issue, please accept the answer. – smartdroid Jun 28 '20 at 23:37
  • Hey, thanks for the help, I think that this solution was about to fix the problem, but unfortunately not this time, I am getting "" empty, nothing after doing: $request_body_raw_data = file_get_contents('php://input'); $data = json_decode($request_body_raw_data , true); // because it's coming as json from javascript $email = isset($data['email']); $message = isset($data['message']); – Mathew Jun 28 '20 at 23:38
  • Oh, sorry for placing this inside the comment. – Mathew Jun 28 '20 at 23:39
  • I've added isset function because I am getting this warning from PHP: Trying to access array offset on value of type null in ... – Mathew Jun 28 '20 at 23:39
  • You have introduced a different error with isset(). You need to use if or ternary operator with isset(). Use var_dump($data); to check what is inside the array. – smartdroid Jun 28 '20 at 23:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216852/discussion-between-smartdroid-and-mathew). – smartdroid Jun 28 '20 at 23:44
  • Yes, that worked, but one thing is that we should use true as the second argument of the json_decode function to make sure that it will return an array – Mathew Jun 28 '20 at 23:56