I am trying to use Ajax to implement a php function in my html file (that contains javascript). I am trying to get all files locally that are JSON files. I tried my best to follow this: http://tinyurl.com/n7zttd9 but I am having trouble. For some reason, I am getting undefined in my alert statement (which is the closest thing I could get to a print statement so far, but if there are other options to print, I am very open to them). Here is some of my code for my local html file:
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function AjaxCaller(){
var xmlhttp;
try{
// for firefox, safari, opera
xmlhttp = new XMLHttpRequest();
}catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
alert("Your browser broke!");
}
}
}
if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function callPage(url, div){
ajax=AjaxCaller();
ajax.open("GET", url, true);
ajax.onreadystatechange=function(){
if(ajax.readyState==4){
if(ajax.status==0){
div.innerHTML = ajax.responseText;
}
}
}
ajax.send(null);
}
//-->
</script>
<script id='code-js' type="text/javascript">
/*...*/
function load(){
var jsonstuff = callPage('findjson.php', document.getElementById('checkjson'));
alert(jsonstuff);
Scene.loadObject(jsonstuff);
}
/*...*/
</script>
Here is my php code (file is findjson.php locally)
<?php
function checkjson() {
foreach (glob("*.json") as $filename) {
return $filename;
}
?>
I used an ajax.status code of 0 because I found online that it has to be 0 for locally. I put alert statement at almost every line, and it seemed to pass through all code ok (like it had an status of 0 and a readyState of 4). I think my Id for document.getElementById is wrong, but I'm not sure what else to put. I know in the url I posted that they used targetId, but I don't think I should use it since I don't define it.
Please let me know if anything is unclear. Thank you so much :)