views:

681

answers:

10

Hello,

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.

+1  A: 

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.

angus
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?
Joshxtothe4
+1  A: 
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>
dylanfm
even when I append the question mark, and change from language to type, it still will not get called.
Joshxtothe4
+1  A: 

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

Paul Tomblin
+1  A: 

Hi,

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"

Chris Kimpton
+1  A: 

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

Chris Kimpton
+2  A: 

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.

Aaron Palmer
A: 

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

dylanfm
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.
Joshxtothe4
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
+2  A: 

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;
}
Shreef
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?
Joshxtothe4
+1  A: 

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?

Svante
A: 

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>

acrosman