0

I have being trying to search this here on Stackoverflow, but because I do not know what it is called I am having trouble.

How do I do this?:

My Javascript code looks like this:

return fetch('http://www.mywebsite.com/backend.php', {
    method: "post",
    headers: {
        'content-type': 'application/json'
    },
    body: JSON.stringify({
        myFloat: 3.14,
        myString: "Hello"
    })
})
.then((response) => response.json())
.then((data) => {
    // How do I get these?
    let returnFloat = ???;
    let returnString = ???;
});

So what does my php file look like?

<?php
    $myFloat = ?????;
    $myString = ?????;

    $returnFloat = 2.0 * $myFloat;
    $returnString = $myString . " and Goodbye"; // "Hello and Goodbye"

    // Now how do I pack this up to send it back???
    // I want to send back $returnFloat and $returnString
?>

So my questions are these:

  1. How do I set $myFloat = ???; and $myString = ???; making sure these are a number and a string respectively (and not both strings)?

  2. In the php I have the line // Now how do I pack this up to send it back???. I want to send back $returnFloat and $returnString. How do I pack them up to send back?

  3. Once these two bits of php data in (2) are sent back, how do I unpack it back in my javascript with the lines let returnFloat = ???; and let returnString = ???; making sure both are a float and a string respectively?

  4. Are there any issues of cross-website security I need to know about? Ie htaccess or the windows server equivalent. If there are, what do they look like?

Rewind
  • 2,554
  • 3
  • 30
  • 56
  • [Read the JSON request in PHP](https://stackoverflow.com/a/18867369/476), output your values as JSON like `echo json_encode(['returnFloat' => $returnFloat, ...])`, which will then be available in your `then((data) => ...` as `data.returnFloat`. – deceze Jul 01 '23 at 16:31
  • Thanks for the comment. I still cannot work out how to do [1]. I think the answer to [2] is `$data = [ "returnFloat" => $returnFloat, "returnString" => $returnString]; header('Content-type: application/json'); echo json_encode( $data );` I think the answer to [3] is `let returnFloat = data.returnFloat; let returnString = data.returnString;`. I still do not know the answer to [4]. Are my assumed answers correct? Can you give me a hint for [1] and [4]? – Rewind Jul 01 '23 at 17:45
  • I not only gave you a hint, I linked to another Q&A with plenty of detail… And [4] really is a different topic altogether you would need to clarify more. – deceze Jul 01 '23 at 18:28
  • Its not 100% crystal clear as there appears to be an argument between two options. Is this correct? `$obj = file_get_contents('php://input'); $myFloat = $obj->myFloat; $myString = $obj->myString;` – Rewind Jul 01 '23 at 22:02
  • You’re missing the `json_decode` in there. – deceze Jul 02 '23 at 06:11
  • Thank @deceze . So its `$json = file_get_contents('php://input'); $obj = json_decode($json,false); $myFloat = $obj->myFloat; $myString = $obj->myString;` with the `false` to convert the `$json` to an object rather than an array (since I am using `->`))? Is that right about `false`? – Rewind Jul 04 '23 at 07:38
  • Yes: https://www.php.net/json_decode – deceze Jul 04 '23 at 07:47

0 Answers0