How to solve a PHP array with Json and Javascript

advertisements

Can anybody help me with this problem ? I am trying to display the array that I retrieve from PHP in HTML / Java form.

<?php
header("Content-Type: application/json; charset=UTF-8");
require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

$API->connect('1.1.1.1', 'admin', '123');
$API->write('/ip/firewall/filter/print');

$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);

echo json_encode ($ARRAY);
$API->disconnect();

?>

output

[{ ".id":"*6", "chain":"unused-hs-chain", "action":"passthrough", "log":"false", "disabled":"true"},

{ ".id":"*5", "chain":"input", "action":"accept", "log":"false", "disabled":"true"},

{ ".id":"*2A", "chain":"unused-hs-chain", "action":"drop", "log":"false", "disabled":"true"}]

displayjava.html

 <tbody id="myTable">
                    <script>
                        var xmlhttp = new XMLHttpRequest();
                        var url = "testdata2.php";

                        xmlhttp.onreadystatechange=function() {
                            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                myFunction(xmlhttp.responseText);
                            }
                        }
                        xmlhttp.open("get", url, true);
                        xmlhttp.send();

                        function myFunction(response) {
                            var arr = JSON.parse(response);
                            var i;
                            var outp = "<tbody>";

                            for(i = 0; i < arr.length; i++) {
                                outp += "<tr>" +
                                "<td>" + arr[i].id + "</td>" +
                                "<td>" + arr[i].chain + "</td>" +
                                "<td>" + arr[i].action + "</td>" +
                                "<td>" + arr[i].log + "</td>" +
                                "<td>" + arr[i].disabled + "</td>" +
                                "</tr>";
                            }

                            outp += "</tbody>"
                            document.getElementById("myTable").innerHTML = outp;
                        }
                    </script>

                    </tbody>

By the way I am displaying the data in a table form format in html file


At least two problems there:

  1. You're outputting invalid JSON:

    [
        {
            "Name": "*6",
            "City": "unused-hs-chain",
            "City2": "unused-hs-chain",
            "City3": "unused-hs-    chain",
            "Country": "passthrough"
        }{
            "Name": "*5",
            "City": "input",
            "City2": "input",
            "City3": "input",
            "Country": "accept"
        }{
            "Name": "*2A",
            "City": "unused-hs-chain",
            "City2": "unused-hs-ch
    
    

    You need commas between those objects (after each } and before the next {).

    Don't generate the JSON manually. Instead, just build up an array of what you want to send back in PHP, then use json_encode so it handles the details for you.

  2. You're using properties on the objects in the array that aren't in the JSON. Your code usese arr[i].id, arr[i].chain, and arr[i].action, but the objects in your JSON don't have id, chain, or action properties, they have Name, City, City2, and so on.