我的Ajax GET无法正常工作。

This is my first encounter with Ajax and I'm also new to . . . everything!

Eventually I want to get values out of a database, but just to check I've got Ajax working, all I'm trying to do is get it to display a line of text.

Here is my code:

function loadSection(section)
{
if (section=="")
  {
  document.getElementById("entry").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("entry").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getentry.php&q="+section,true);
xmlhttp.send();
}

And here's the PHP:

<?php

$section = $GET[$section]; //not currently doing anything, will be needed when     
actually searching database

echo "fingers crossed";


?>

As far as I can tell the only thing I'm doing different to the example on the W3Schools site it putting the Ajax in a separate .js file, but it also doesn't work when I include it in the main page.

Any help much appreciated!

Edit to add: thanks guys! jraede correctly figured out the function was never getting called, and the jquery tip-off is also appreciated :-)

you should writ like this-

$section = $GET['q'];

is not getentry.php&q=,

try with

getentry.php?q=

And in $_GET should be $_GET['q']

If you are using chrome, open console and see if there is any errors.

You should look into jQuery

You could simple use .get() to fetch the required information.

Example of $.get()

    $.get( "getentry.php?q=" + section, function( data ) {
       // log response in console.
       console.log(data);
    });

There are a few things that could be going wrong here, so it would help to have more information. However, going from what you have provided, I can identify the following areas that you should check:

1) Make sure that your syntax is correct in both javascript and PHP.

Your javascript has a few errors that people have already pointed out, namely that "getentry.php&q=" should be "getentry.php?q=".

In your PHP, you are trying to retrieve $GET[$section], which has two errors. First, to retrieve variables from the query string, you need to use $_GET, not $GET. Second, assuming $section isn't yet defined or, at least that there is no matching variable in the query string, this code would return a blank value. As many have already pointed out, this should be $_GET['q'].

2) Make sure your script is doing what you think it's doing.

Where are you calling your loadSection() function? Are you calling it at all? Try using alert or console.log to spit out the value of section within that function - maybe it's not being set?

3) Make sure the request is going out, and check if you are getting a response

With Chrome you can open up the developer tools, click on the network tab, and watch AJAX requests go out and view their response. Open up the network tab, refresh the page, do whatever you need to do to call your loadSection() function, and then click on the "preview" or "response" tab to see what your server has responded. It could be that the actual AJAX request is working fine and it's just not doing anything with the response.

4) Make sure that your response is acted upon

I see that you have code to set the innerHTML of #entry with whatever the response text is. Make sure there is an element with id="entry" somewhere on the page, and that it is on the page at the time that this code is run. Also, try using console.log on the xmlhttp object in your onreadystatechange function to see what it is doing. It could be that the status code is never set to 200 or the readyState is never set to 4.