<?php
if(isset($_GET['getjson'])) {
$arr = array(
array (
"name" => "Moon trance",
"artist" => "Lindsey Stirling",
"length" => "452"
),
array (
"name" => "Crystalize",
"artist" => "Lindsey Striling",
"length" => "349"
)
);
die(json_encode($arr));
};
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSON Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function doJson() {
//Notify users
$("#text").html("Fetching...");
$.get("<?php echo basename($_SERVER['PHP_SELF']); ?>?getjson=1", function(data) {
var obj = JSON.parse(data);
for(var i = 0; i < obj.length; i++) {
$("#text").append(
"<p>"+(i+1)+":<br>" +
"name: " + obj[i].name + "<br>" +
"artist: " + obj[i].artist + "<br>" +
"length: " + obj[i].length + "<br></p>"
);
}
});
}
function decodeHtml(x) {
var y = document.createElement("textbox");
y.innerHTML = x;
return y.value;
}
</script>
</scipt>
</head>
<body>
<input type="button" onclick="doJson()" value="Do JSON" /><br />
<p id="text">
</p>
</body>
</html>
Comments