Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
window.alert("x,jsondata="+x);
var jsondata = JSON.parse(x);
document.getElementById("1").innerHTML = jsondata[0].1;
document.getElementById("2").innerHTML = jsondata[0].2;
document.getElementById("3").innerHTML = jsondata[0].3;
document.getElementById("4").innerHTML = jsondata[0].4;
document.getElementById("5").innerHTML = jsondata[0].5;
document.getElementById("6").innerHTML = jsondata[0].6;
document.getElementById("7").innerHTML = jsondata[0].7;
document.getElementById("8").innerHTML = jsondata[0].8;
document.getElementById("9").innerHTML = jsondata[0].9;
xhttp.open("GET", "getData.php?q="+<?php echo $fileToAccess;?>, true);
xhttp.send();
This is getData.php:
$file=$_REQUEST['q'];
$myfile=file_get_contents($file);
$json=json_decode($myfile);
echo $json[1];
and this is how my json file looks like:
[{"str": "user2"},{"1": "","2": "","3": "","4": "","5": "","6": "","7": "","8": "","9": ""}]
I added a timer to constantly update the values of my buttons with the help of the json file. But I am getting the error at line
document.getElementById("1").innerHTML = jsondata[0].1;
–
You're pretty right in what you're doing. But since the key is numeric, you have to fetch it with ['1']
instead. Like this:
document.getElementById("1").innerHTML = jsondata[0]['1'];
In Javascript there's mainly two ways to get a property from an object
obj.prop
obj['prop']
Generally you can say that obj.prop
will only work if the property is valid variable name (doesn't contain special characters, numeric, etc).
–
–
–