0

I need a PHP code to return an array object inside a Javascript block, which is inside my home.html page. Here's the PHP code:

<?php
$fields=array('Lat','Lon');
$nodedata = array_fill_keys($fields, '');

$nodedata[0]= array("Nome" => "Lat1" , "Cognome" => "Lon2");
$nodedata[1] = array("Nome" => "Athens" , "Cognome" => "Belgrade");
$nodedata[2] = array("Nome" =>  "Athens" , "Cognome" => "Belgrade");
echo json_encode($nodedata);
?>

This is file home.html:

<html>
<head>
</head>
<body>

<div id="id01">
</div>
<script type='text/javascript'>

function prova2() {

    var xmlhttp = new XMLHttpRequest();
    var url =  "prova.php";
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//var app=myFunction(xmlhttp.response);
// document.getElementById("id01").innerHTML=app; Fino a qui stampa tutto
           return myFunction(xmlhttp.response);
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();

    function myFunction(response) {
        var arr = JSON.parse(response);
        var result = "";
        for (var a in arr){
            if (arr[a].Lat != undefined) {
                result += "Nome : " + arr[a].Nome + " Cognome: " + arr[a].Cogmone + "<br> ";
            }

        }
       // document.getElementById("id01").innerHTML=result; Fino a qui stampa tutto
    }

}

function prova3() {
var app2=prova2();
document.getElementById("id01").innerHTML=app2; //Qui non mi stampa nulla

}
</script>
</body>
</html>

Anyway, the instruction

document.getElementById("id01").innerHTML=app2;

fails and nothing in displayed in browser page. Can you help me? Thanks.

ubisum
  • 179
  • 3
  • 12

0 Answers0