Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Cant fix the JavaScript runtime error

    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!

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Try swapping this in for the GetXmlHttpObject function

    Code:
    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;
     }
    }
    Last edited by rwedge; 08-11-2007 at 11:01 PM. Reason: paste error

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Your code should be checking for the status==200. You are probably getting a 404 or 500 error from the server.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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..

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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'

  • #6
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you

    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •