Thingfish47 Posted August 2, 2016 Share Posted August 2, 2016 In php I have extracted 20 items from the db, and can display them fine. I drop out of php into javascript and execute the following line of code (straight from examples...) var kArray = <?php echo json_encode($Keys); ?>; I have also tried var kArray = <?php echo json_encode($Keys,JSON_FORCE_OBJECT); ?>; kArray.length is undefined, and are all entries. Do I need to install more software for this to work? I have PHP 7 Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/ Share on other sites More sharing options...
Jacques1 Posted August 2, 2016 Share Posted August 2, 2016 It's wrong and dangerous to execute JSON-encoded data as JavaScript code. Use Ajax or HTML-escape the encoded data and put it into a hidden HTML element where it can be parsed with JavaScript. We'll also need to see the content of $Keys if you want help with that. Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/#findComment-1535457 Share on other sites More sharing options...
Thingfish47 Posted August 2, 2016 Author Share Posted August 2, 2016 $Keys is an array containing 20 integers. It all debugs out to console.log correctly. There are NO security issues with this, it will never get out of the lan, and runs on my personal development PC, but I'm happy to learn. All I really want to do is get hold of the data as quickly and easily as possible. This is not my main area of expertise, and almost certainly never will be. Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/#findComment-1535459 Share on other sites More sharing options...
Jacques1 Posted August 2, 2016 Share Posted August 2, 2016 (edited) You can literally copy and paste the example code from the above thread: <?php header('Content-Type: text/html; charset=UTF-8'); // test data $keys = [3, 42, 99]; ?> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>JSON</title> <style> .hidden { display: none; } </style> </head> <body> <div id="my-data" class="hidden"><?= htmlspecialchars(json_encode($keys), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8'); ?></div> <script> var kArray = JSON.parse(document.getElementById('my-data').innerHTML); alert('Array content: ' + kArray); </script> </body> </html> Edited August 2, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/#findComment-1535462 Share on other sites More sharing options...
Thingfish47 Posted August 2, 2016 Author Share Posted August 2, 2016 That code works fine. Many thanks! I just need to implement it within my code. Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/#findComment-1535464 Share on other sites More sharing options...
Thingfish47 Posted August 2, 2016 Author Share Posted August 2, 2016 I really am sorry for troubling you guys. Adding the word 'global' inside the function that extracted the data has worked wonders. DOH! Quote Link to comment https://forums.phpfreaks.com/topic/301724-json_encode-hell/#findComment-1535466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.