0

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

1
  • The first thing to do is: 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. Commented Dec 16, 2014 at 7:19

3 Answers 3

3

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.

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

1 Comment

Thanks for the help. I using the json_encode to compile into array. Now what should i do so i can display in the html side ?
1

Why not just use json_encode() instead of building it manually.

$output = array();
foreach($ARRAY as $rs) {
    $output[] = array(
        'Name' => $rs['id'],
        'City' => $rs['chain'],
        'City2' => $rs['chain'],
        'City3' => $rs['chain'],
        'Country' => $rs['action'],
    );
}

echo json_encode($output);
exit;

Your JSON string response right now has missing {}, commas.

Comments

1

Don't build manually use json_encode() function ,

Above code is

<?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);

$outp = array();
foreach($ARRAY as $rs) {
    $outp[] = array(
        'Name' => $rs['id'],
        'City' => $rs['chain'],
        'City2' => $rs['chain'],
        'City3' => $rs['chain'],
        'Country' => $rs['action'],
    );
}


$API->disconnect();

echo json_encode($outp);
?>

1 Comment

if your going to copy the foreach part make sure you also change the variable name, it makes it obvious

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.