...

Cant fix the JavaScript runtime error

evie
08-11-2007, 03:36 PM
I am trying to implement a search function with Ajax and PHP when clicking a button, a hidden div showing the results of a database query should be shown. Technically, the code is following:

HTML: <input type="button" onclick="JavaScript:showresults(search_ing.value)" value="search">
JavaScript:
function showresults(item_name)
{
xmlHttp=GetXmlHttpObject()
var url="/manager/ingredients/showresults.php"
url=url+"?item_name="+item_name
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{

document.getElementById("result").style.display="block";
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

However, I have a continuing problem in IE. An alert message pops up stating:Unknown JavaScript Error, indicating the line i have made bold in the code?
new ActiveXObject("Msxml2.XMLHTTP") instead of new ActiveXObject("Microsoft.XMLHTTP") but that was no help... Please help me!

rwedge
08-11-2007, 07:24 PM
Try swapping this in for the GetXmlHttpObject function


var objXMLHttp;
function GetXmlHttpObject() {
try{
objXMLHttp=new XMLHttpRequest();
} catch (e){
try{
objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (! objXMLHttp) {
alert('Your Browser Does Not Support This Technology');
return;
}
}

A1ien51
08-12-2007, 04:09 AM
Your code should be checking for the status==200. You are probably getting a 404 or 500 error from the server.

Eric

evie
08-13-2007, 01:00 AM
A1ien51, I tried it and the status is 200 -so I guess everything is OK with the server...
Still nothing has changed in my situation, even when i tried rwedge's suggestion.. :(

rwedge
08-13-2007, 07:51 PM
In my example the variable objXMLHttp is declared globally so the other function 'showresults' has access to it.

In your posted code you have a local scope applied by using 'var objXMLHttp' in the function GetXmlHttpObject and it would be 'undefined' for 'showresults'

evie
08-14-2007, 03:55 AM
rwedge, you are absolutely right, the variable had to be global! What a stupid mistake!

Now everything works fine...Thank you very much, I really appreciate!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum