1

I am using this HTML

<html>
    <head>
        <Title>EBAY Search</title>
    </head>
    <script language="JavaScript" src="ajaxlib.js"></script>
    <body>
        Click here <a href="#" OnClick="GetEmployee()">link</a> to show content
        <div id="Result"><The result will be fetched here></div>
    </body>
</html>

With this Javascript

var xmlHttp

function GetEmployee()

{

xmlHttp=GetXmlHttpObject()

if(xmlHttp==null)

{

alert("Your browser is not supported")

}

var url="get_employee.php"

url=url+"cmd=GetEmployee"

url=url+"&sid="+Math.random()

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

function FetchComplete()

{

if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

{

document.getElementById("Result").innerHTML=xmlHttp.responseText

}

if(xmlHttp.readyState==1 || xmlHttp.readyState=="loading")

{

document.getElementById("Result").innerHTML="loading"

}

}

function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

try

{

xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}

However it is not being called. get_employee.php works fine when I call it by itself, so that is not the problem. Is there anything wrong in my code that would prevent it from being called? I cannot test with any firefox extensions, I do not have access, so please don't give that as an answer.

edit: the problem is the javascript is not being called at all. I fixed the question mark problem, but even just a simple javascript with an alert is not being called.

3
  • ken: Why did you feel the need to edit?
    – user1253538
    Nov 20, 2008 at 12:52
  • @Johsxtothe4 - consistency. JavaScript should be JavaScript
    – Ken
    Nov 20, 2008 at 13:18
  • I don't see what causes FetchCommplete to be called. Shouldn't there be onload in there somewhere? Also, check your server logs to see if ajaxlib.js and get_employee.php are ever getting fetched. Nov 20, 2008 at 13:22

10 Answers 10

1

use a javascript debugging tool like firebug, this will make your life simpler.

you had a syntax error in your code that made the error "GetEmployee is not defined"

it was a missing "catch" after the last try in "GetXmlHttpObject()". this is the same function after adding the missing "catch".

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }catch (e)
    {

        try
        {
            xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}

    }
return xmlHttp;
}
1
  • I don't have access to firebug, but your fix seems to be working, however instead of an error now nothing happens..is the php not being called for some reason?
    – user1253538
    Nov 20, 2008 at 13:27
1
var url="get_employee.php?"

Needs the "?".

It's better to use this markup to include your scripts:

<script type="text/javascript" src="ajaxlib.js"></script>
1
  • even when I append the question mark, and change from language to type, it still will not get called.
    – user1253538
    Nov 20, 2008 at 13:02
1

Change this

var url="get_employee.php"

url=url+"cmd=GetEmployee"

url=url+"&sid="+Math.random()

to this:

var url="get_employee.php?cmd=GetEmployee&sid="+Math.random();

You were missing the "?" and there's no need for all of the concatenation (but I guess that's just personal style).

Also, if you actually have the "<The result will be fetched here>" in your html, you should remove it.

1

I am a bit confused about putting the <script> tag into the no man's land between head and body. Does this have some special meaning?

0

Shouldn't there be a question mark or an ampersand between the getcommand.php and the cmd= parts?

0

I don't suppose its something silly like a missinq question mark in the url

var url="get_employee.php"
url=url+"cmd=GetEmployee"
url=url+"&sid="+Math.random()

I would have expected to see "?cmd=GetEmployee"

0

Make sure you aren't misplacing or naming a file. You can change OnClick to all lowercase too.

2
  • ajaxlib.js is in the same directory as the html file, and is definitely the same name as called from the html file. changing to lowercase did not help.
    – user1253538
    Nov 20, 2008 at 13:10
  • I'm guessing you've tried alert('blah'); at the top of the file? If the file isn't getting executed, maybe it's a CHMOD issue. A note: add a return false; where the alert saying xmlhttprequest isn't supported. You should put together a thorough getxmlhttprequest function.
    – dylanfm
    Nov 20, 2008 at 13:19
0

var url="get_employee.php" + "?"

(answering the comment)

What error is reported? You still have to attach your FetchComplete function to xmlHttp's "onreadystatechange" property, but it shouldn't be an error not to do it.

Make sure that ajaxlib.js is really loaded, and that it is the file you mean. Put some alerts and see if they pop up.

1
  • even when I append the question mark, it is still not called. there is a reported problem at line 7 of the html file, is there a problem with OnClick?
    – user1253538
    Nov 20, 2008 at 13:02
0

If you can't use a proper debugger, you can add alert statements all over the place to see if anything is happening at all (yes, it's a bad solution, but anytime you don't use a good tool you have a bad solution).

Also, make sure the OnClick() returns false, to prevent the browser from reloading the page.

<a href="#" OnClick="GetEmployee()">link</a>

becomes

<a href="#" OnClick="GetEmployee(); return false;">link</a>

0

You can use alert(url) to check the exact url being sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.