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.
Open in new window