1

I am currently using HTML/PHP to get a JSON file from an API. I then want to manipulate the JSON file in JavaScript and display certain elements of the JSON on the page. I can see that the JSON is being bought back correctly, but I cannot save the JSON into a JavaScript variable after this. My code is below.

<?php

$pokemon = $_POST['pokemon'];
$siteaddressAPI = "http://pokeapi.co/api/v1/game/" . $pokemon . "/";
$data = file_get_contents($siteaddressAPI);

echo($data)

?>

<!DOCTYPE HTML>
<html>
<body>
<br><br><br><br><br><br><br>
<p>Created: <span id="created"></span><br></p> 
    <script>
        var txt = <?php echo ($data); ?>;
        obj = JSON.parse(txt);
        document.write("<p>Created: " + obj.created + "</p>");
    </script>
</body>
</html>

EDIT: I have just noticed my lack of ' ' around my txt variable, this has now been fixed but the issue remains.

2
  • 2
    You don’t need JSON.parse, remove it. (And rename your variable txt to obj in the first place, because that is what you are getting when your JSON code gets interpreted by the JS engine.) Commented Mar 13, 2014 at 17:16
  • Thats great, thank you! the main issue I have now is that if I document.write or document.getElementById to output certain information in the JSON file, it all comes out as 'undefined'. Commented Mar 13, 2014 at 17:32

1 Answer 1

2

An example of the output of the API is:

{"created": "2013-11-03T19:31:10.975452", "generation": 1, "id": 1, "modified": "2013-11-03T19:31:10.975393", "name": "Red(jpn)", "release_year": 1996, "resource_uri": "/api/v1/game/1/"}

You don’t need JSON.parse, remove it. (And rename your variable txt to obj in the first place, because that is what you are getting when your JSON code gets interpreted by the JS engine.)

var obj = <?php echo ($data); ?>;
document.write("<p>Created: " + obj.created + "</p>");

An example is shown with the console below:

Sign up to request clarification or add additional context in comments.

5 Comments

That's great, thank you! the main issue I have now is that if I document.write or document.getElementById to output certain information in the JSON file, it all comes out as 'undefined'.
So the exact line document.write("<p>Created: " + obj.created + "</p>"); returns Created: undefined? Did you make sure that you didn't have the JSON.parse?
The JSON.parse is gone now. the new line is document.write("<p>Created: " + obj.created + "</p>");.
Can you provide the value of $pokemon? Also make sure the previous line is var obj = <?php echo $data; ?>;, Juan Mendes corrected my typo.
The value of Pokemon is any number between 1 - 15. After removing he brackets and apostrophes from var obj = '<?php echo ($data;) ?>'; it now works, and is successfully allowing me to display the JSON data on screen the way I want. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.