Avatar of Computer_Dilo
Computer_Dilo
 asked on

Ajax Javascript getElementById error - Emprty String Passed

I am using the following code to get Ajax to update a series of combo boxes based on the input of previous boxes. the javascript calls a PHP database lookup script...

var xmlHttp;

function pop_model(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax/pop_model.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged("optModel");
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

// Check for PHP script completed
function stateChanged(blk)
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
  document.getElementById(blk).innerHTML=xmlHttp.responseText;
 }
}

// Check if appropriate browser is in use for AJAX and determine best method to suit
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

...HTML target is defined via...

                <div id="optModel">
                  <select name="cbModel" class="formbox1" id="cbModel" onChange="pop_variant(document.catsearch.cbModel.value)">
                    <option value="0" selected>All</option>
                  </select></div>

The script runs and the PHP script returns the expected "Select" output but FireBug shows an error of: Empty string passed to getElementById(). and there is no change to the source.
JavaScript

Avatar of undefined
Last Comment
Morcalavin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Morcalavin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
third

try first checking if the element exists before assigning a value to it.
// Check for PHP script completed
function stateChanged(blk)
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
  var obj = document.getElementById(blk);
  //only assign value when the object exists
  if(obj){
    obj.innerHTML=xmlHttp.responseText;
  }
 }
}

Open in new window

Computer_Dilo

ASKER
Whoo - thanks Morcalavin - that nailed it.

Funny - I only call that getElementById once in the script but the error is still being flagged in FireBug even though the combo box is now working.

Any ideas on that??
Morcalavin

Try changing:
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
to:
if (xmlHttp.readyState==4 && xmlHttp.readyState=="complete")
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Computer_Dilo

ASKER
Doesn't matter - it fricken works. For some reason FireBug wont let go of the old file when I upload the new one to my server. Keeps showing me old code :(
Computer_Dilo

ASKER
Nup that breaks it again. Looks like that one has to be OR :)
Morcalavin

My bad.  I wasn't paying attention.  For some reason I thought it had xmlHttt.status instead of xmlHttp.readyState.  
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.