0

So, here is the issue:

I am trying to handle some edits within a PHP file that is being passed an ID field. The PHP file is successfully building the HTML page and displaying it, but portions of the embedded javascript call are not working.

If I comment out the call to echo ' xmlhttp.onreadystatechange=function(){'; I receive all ALerts.

As soon as I uncomment the xmlhttp.onreadystatechange=function() block, nothing works.

Any help would be appreciated - below is the full code block:

echo '<script type="text/javascript">';
echo 'function populateRoomDropDown(building)';
echo '{';
echo '  alert("Started...");';
echo '  if(window.XMLHttpRequest){';
echo '      xmlhttp = new XMLHttpRequest();';
echo '      alert("Building ID: "+building);';
echo '  }';
echo '  else{';
echo '      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");';
echo '      alert("IE Browser");';
echo '  }';
echo '  xmlhttp.onreadystatechange=function(){';
echo '      alert("Inside onready...()");';
echo '          if (xmlhttp.readyState==4 && xmlhttp.status==200){';
echo '          alert("Inside onready...()");';
echo '          document.getElementById("displayRooms").innerHTML=xmlhttp.responseText;';
echo '          }';
echo '  }';
echo '  alert("Near end...");';
echo '  xmlhttp.open("GET","../db_queries/getRoomsDropDown.php?buildingID="+building,true);';
echo '  xmlhttp.send();';
echo '  alert("End...");';
echo '}';
echo '</script>';
RonTron
  • 50
  • 5
  • 2
    Yikes. Please [read up on heredocs and nowdocs](http://php.net/language.types.string), they'll make this code much easier to work with, if you insist on keeping it `echo`'d. Also, seriously, you really want to use a third-party library for ajax. Don't be afraid of jQuery. – Charles Dec 11 '12 at 22:42
  • Thanks for the input Charles - I'm fairly new to web programming in general, so I will certainly take a look into jQuery (I know a little about it). Is my approach unsound - from HTML page, user wants to edit a DB entry, so I am passing the ID of that DB entry to the php file and displaying it back to the user in the php file - more or less, I am trying to repopulate an HTML 'selection' drop down based on DB callbacks - but can't get the php generated javascript to fire fully. – RonTron Dec 11 '12 at 22:45
  • What does your browser / console / whatever in-browser debug you are using say about the error? – Wrikken Dec 11 '12 at 22:47
  • Not seeing anything obvious, but frankly not sure how to check for those types of errors. Shouldn't it work line by line and output the alerts that are prior to the "xmlhttp.onreadystatechange=function()" call? – RonTron Dec 12 '12 at 01:56

1 Answers1

0

Try:

echo '<script type="text/javascript">';
echo 'function populateRoomDropDown(building)';
echo '{';
echo '  alert("Started...");';
echo '  if(window.XMLHttpRequest){';
echo '      xmlhttp = new XMLHttpRequest();';
echo '      alert("Building ID: "+building);';
echo '  }';
echo '  else{';
echo '      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");';
echo '      alert("IE Browser");';
echo '  }';
echo '  xmlhttp.open("GET","../db_queries/getRoomsDropDown.php?buildingID="+building,true);';
echo '  xmlhttp.onreadystatechange=function(){';
echo '      alert("Inside onready...()");';
echo '          if (xmlhttp.readyState==4 && xmlhttp.status==200){';
echo '          alert("Inside onready...()");';
echo '          document.getElementById("displayRooms").innerHTML=xmlhttp.responseText;';
echo '          }';
echo '  }';
echo '  alert("Near end...");';
echo '  xmlhttp.send();';
echo '  alert("End...");';
echo '}';
echo '</script>';

I moved xmlhttp.open("GET","../db_queries/getRoomsDropDown.php?buildingID="+building,true); above xmlhttp.onreadystatechange=function()

SeanWM
  • 16,789
  • 7
  • 51
  • 83
  • No dice on changing that - which is what I expected as I believe you have to define the function that will wait on a change before opening a request. – RonTron Dec 12 '12 at 01:52