# POST parameters to php

**URL:** <https://forums.fusetools.com/t/post-parameters-to-php/5577>\
**Category:** How-to Discussions\
**Created:** [July 14, 2016, 3:27pm UTC](https://forums.fusetools.com/t/post-parameters-to-php/5577 "2016-07-14T15:27:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benny2871](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/benny2871/32/178_2.png) [@Benny2871](https://forums.fusetools.com/u/Benny2871)\
**Post date:** [July 14, 2016, 3:27pm UTC](https://forums.fusetools.com/t/post-parameters-to-php/5577/1 "2016-07-14T15:27:14Z")

</div>

Hello guys,

I am looking for several hours now, but I cant get a solution, to pull a json object back from a php page which I passed to it before that.

This is my code so far

Javascript in mainview.ux

```auto
        var Observable = require('FuseJS/Observable');
        var status = 0;
        var response_ok = false; 
        var name = Observable("loading...");
        var name2 = Observable("");

        var requestObject = {"name":12,"name2":10}; 

           console.log(JSON.stringify(requestObject));
        fetch('http://192.168.0.12/index.php&#39;, { method: 'POST',
            headers: { "Content-type": "application/json"},
            dataType: 'json', 
            body: JSON.stringify(requestObject)
        }).then(function(response) { status = response.status; // Get the HTTP status code
            response_ok = response.ok; // Is response.status in the 200-range?

            return response.json(); // This returns a promise
        }).then(function(responseObject) {
            name.value = responseObject.name;
            name2.value = responseObject.name2; 
        }).catch(function(err) {
            // An error occured parsing Json
            console.log(err.message); });
        module.exports = {
        };

```

php file

```auto
    <?php

        if($_SERVER['REQUEST_METHOD']=='POST') {

        $jsonData = "php://input";
        $phpJsonString = file_get_contents( $jsonData );
        $data = json_decode( $phpJsonString ); 

        $var = json_encode($data, JSON_FORCE_OBJECT);

        echo($var);
        }
    ?>

```

and the console reaction to it…

```auto
LOG: Unexpected token <
LOG: Unexpected token <
LOG: Unexpected token <
LOG: Unexpected token <

```

If I have an array in the php file and want to read it only, it works. But not when I try to pass it from my app.

Have I missed something?

---

<div class="post-metadata">

**Author:** ![petterroea](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/petterroea/32/438_2.png) [@petterroea](https://forums.fusetools.com/u/petterroea)\
**Post date:** [July 14, 2016, 3:30pm UTC](https://forums.fusetools.com/t/post-parameters-to-php/5577/2 "2016-07-14T15:30:10Z")

</div>

Could you do the request in your web browser or using something like `wget`, and show how the response from the php server looks?

---

<div class="post-metadata">

**Author:** ![Benny2871](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/benny2871/32/178_2.png) [@Benny2871](https://forums.fusetools.com/u/Benny2871)\
**Post date:** [July 14, 2016, 4:47pm UTC](https://forums.fusetools.com/t/post-parameters-to-php/5577/3 "2016-07-14T16:47:58Z")

</div>

I made a small test script, it looks like this

```auto
<?php

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

$data=json_encode($array, JSON_FORCE_OBJECT); 
$encoded = htmlentities($data);

echo '
<form method="POST" action="index.php">

<input type="hidden" name="array" value="'.$encoded.'">
<input type="submit" name="submit" value="go!" />

</form>';

?>

```

and changed my actual php script to this

```auto
<?php

if($_SERVER['REQUEST_METHOD']=='POST') {

$jsonData = "php://input";
$phpJsonString = file_get_contents( $jsonData );
$data = json_decode( $phpJsonString );

echo($_POST['array'] . "<br>");

$var = json_encode($data, JSON_FORCE_OBJECT);

echo($var);
}
?>

```

The echo post with break is made to test if there is an output at all. The output is valid json, but this doesnt work with fuse post. In the second line the output of $var is null, $data shows nothing and $phpJsonString shows weird numbers and html structures.

```auto
{"foo":"bar","bar":"foo"}
null

```
