views:

171

answers:

5

hello,

i have a html file calling 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");
        } 
        catch (e) {}

    }
return xmlHttp;
}

Which works fine, however my PHP file seems not to be displaying any result, the code is here:

<?php
session_start();
if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"];
else
  die("You should have a 'cmd' parameter in your URL");
$con = mysql_connect("localhost","root","geheim");
if(!$con)
{
die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);
if($cmd=="GetEmployee")
{
echo "<table border='1' width='100%'>
<tr>
<th>test1</th>
<th>test2</th>
<th>test3</th>
</tr>";
$sql="select * from tblAuction";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['SELLER_ID']."</td>
<td>".$row['ACCESSSTARTS']."</td>
<td>".$row['ARTICLE_NAME']."</td>
</tr>";
}
echo "</table>";
}
mysql_close($con);
?>

the php page seems to be being called, as my apache log shows:

127.0.0.1 - - [20/Nov/2008:14:29:22 +0100] "GET /Employee.html HTTP/1.1" 304 -
127.0.0.1 - - [20/Nov/2008:14:29:22 +0100] "GET /ajaxlib.js HTTP/1.1" 304 -
127.0.0.1 - - [20/Nov/2008:14:29:23 +0100] "GET /get_employee.php?cmd=GetEmployee&sid=0.7152559213961012 HTTP/1.1" 200 282
127.0.0.1 - - [20/Nov/2008:14:29:24 +0100] "GET /get_employee.php?cmd=GetEmployee&sid=0.15027097582792692 HTTP/1.1" 200 282
127.0.0.1 - - [20/Nov/2008:14:29:24 +0100] "GET /get_employee.php?cmd=GetEmployee&sid=0.24347586051168096 HTTP/1.1" 200 282

The php file works fine when called by itself.

+4  A: 

You need xmlHttp.onreadystatechange = FetchComplete;

And please stop posting this code over and over!

Greg
It was a new question, what is wrong with pasting it again, how does it bother anyone?
Joshxtothe4
+3  A: 

Instead of writing your own AJAX wrapper (which seems to be causing you headaches), you should try a library like jQuery. Those libraries have already worked out things like how to deal with the callbacks elegantly. I'd expect that you'll make fewer mistakes and have a better result.

acrosman
+2  A: 

I recommend you install firebug (getfirebug.com) - using its console you can view the data returned by an AJAX request.

You would have quickly seen that actually, something is being returned, its just that nothing is being done with it afterwards. (which is what I assume, for the same reason as RoBorg makes his comment)

benlumley
A: 

As I said the first time you asked this question, I don't see anything in your code that does anything when the XML comes back. Specifically, there is nothing there to call FetchComplete.

Paul Tomblin
A: 

Three things you need to do:

  • Use JSON as the format for passing data
  • Use jQuery to actually retrieve the data
  • Use json_encode() in the PHP server page to dump the data

You'll learn something new and become more productive at the same time!

csl