views:

130

answers:

4

Hello everyone,

I have a simple search form with a search box and a result box.

When I type a search word a request is created like: http://www.site.com/php_handler.php?s=hello In the php script and a result is given back to the script this way:

<?php return $s; ?>

The problem is that my htmlrequest stops at readyState 3 it doesn't get to 4.

The javascript looks like this:

var xmlhttp = sajax_init_object();

function sajax_init_object() {
     var A;
     try {
      A=new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       A=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (oc) {
       A=null;
      }
     }
     if(!A && typeof XMLHttpRequest != "undefined")
      A = new XMLHttpRequest();
     if (!A)
      sajax_debug("Could not create connection object.");
     return A;
    }
function getSearchItem()
  {
     gs=document.forms.mainform.resultsfield;
     var searchword=document.forms.mainform.searchform.value;
     if (searchword.length>=3)
     {
     setWaitCursor();
     clearResults();
     var uri = "http://site.com/ajax_handler.php?s="+searchword;
     console.log(uri);
     xmlhttp.open("GET", uri, true);
     xmlhttp.onreadystatechange=function() 
     {

        if (xmlhttp.readyState==4) {
          processResults(xmlhttp.responseText);
         removeWaitCursor();
        }else{
      console.log(xmlhttp.readyState);
     }
      }
      xmlhttp.send(null);
    }
    else
    {
      alert("please add at least 3 characters ."); 
    }    
  }

Can someone tell me why it stops at 3?

edit: here is also the php code:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

session_start();


//include main file
require_once($_SESSION["FILEROOT"] . "xsite/init.php");

//check if formulier is posted

$zoekterm = C_GPC::getGETVar("s");
$s="";

if ($zoekterm != "") {
    $query="SELECT number,name,city,zib,zip_annex FROM articles WHERE version='edit' AND (naam LIKE '%$school%' OR brinnummer='$school')  ORDER BY name";
    if ($rs=C_DB::fetchRecordSet($query)) {
        while ($row=C_DB::fetchRow($rs)) {
            if ($row["plaats"]!="") {
                $s.=$row["name"].", ".$row["city"]."|".$row["number"]."\n";
            } else {
                $s.=$row["name"].", ".$row["zip"].$row["zip_annex"]."|".$row["number"]."\n";
            }
        }
    }
}

return $s;


?>

edit:

I missed a semicolon in my php script and now the ready state only gets to 2

edit:

The problem is even different. It gets to 4 but it doesn't show the result text.

A: 

Are you ending the output on your PHP page... For example, if you use

ob_start();

Are you also flushing the output, such as...

ob_end_flush();
Sohnee
Not at all. I will post the php in the start post. wait one sec.
sanders
Thank for posting that - create the URL used by your AJAX request and post it straight into a browser to view the output. My immidiate suggestion would be to echo $s rather than return $s.
Sohnee
A: 

ReadyState 3 => Some data has been received

ReadyState 4 => All the data has been received

Maybe the XMLHTTPRequest object is still waiting for some data.

Are you sure your php script ends correctly ?

Is the content-length alright ?

Sébastien Nussbaumer
when I do the query straight in the db i get 4 records on my search term ;-). How can I debug the php code in an ajax request?
sanders
A: 

To debug this you have two options, type the URL directly into the browser [since you are using a GET] and see what is happening.

OR

You can use a tool such as Fiddler and see what is exactly happening with the XMLHttpRequest

epascarello
A: 

1> Don't send Cache-Control: post-check=0, pre-check=0. These don't do what you think they do, and they're entirely unnecessary.

2> Your AJAX results page needs to send a Content-Length or Connection: Close header.

3> Try adding a random to your request URL to ensure you're not looking at a stale cache entry.

EricLaw -MSFT-