ajax readyState 1 wall

G

gp

This code is stopping ...I can't get past readyState == 1....
for testing purposes my searchDB.php is simply:
<?php
echo "string blahahahahah blahahaha hahahahhb ahaha";
?>

newK is <input id> it is dynamically created with php at the time the
form loads...there up to 28 <input>
that will have the google type search suggestion <div>. srchK is the
<div id> generated for each corresponding <input>

var xhr;

function XHR() {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject(
"Microsoft.XMLHTTP");
}
return xhr;
}

function searchDB(newK, srchK) {
var http = XHR();

var str = escape($(newK).value);
http.open('POST', 'http://my.url.here/searchDB.php', true);
http.setRequestHeader("Content-type",
"application/x-www-form-urlencoded; charset=UTF-8");
http.onreadystatechange = handleSearchSuggest(http, newK,
srchK);
http.send(str);
}

function handleSearchSuggest(http, newK, srchK) {
var xhttp = http;

if (xhttp.readyState == 4) {
var ss = $(srchK);
ss.innerHTML = '';
var str = xhttp.responseText;//.split("\n");
var estr = eval(str);
alert (estr);
for (var i=0; i < estr.length - 1; i++ ) {
alert (i);
var suggest = '<div
onmouseover="javascript:suggestOver(this):" ';
suggest += 'onmouseout="javascript:suggestOut(this);"
';
suggest +=
'onclick="javascript:setSearch(this.innerHTML, '+newK+', '+srchK+');"
';
suggest += 'class="suggest_link">' + str + '</div>';
ss.innerHTML += suggest;
}
}
 
M

Michael Winter

gp wrote:

[snip]

A global variable to which you assign, and yet return from a function? Odd.
function XHR() {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject(
"Microsoft.XMLHTTP");

You aren't worried about MSIE throwing an exception if ActiveX is
disabled? Consider an alternate implementation - there are examples in
the group archives[1].

[snip]
var str = escape($(newK).value);

You aren't using Prototype, are you?

[snip]
http.onreadystatechange = handleSearchSuggest(http, newK,
srchK);

Here, you are calling the handleSearchSuggest function, and assigning
its return value to the onreadystatechange property. You either need to
assign a reference directly:

http.onreadystatechange = handleSearchSuggest;

and modify the function appropriately, or create another function which
takes its place and called handleSearchSuggest with the appropriate
arguments.

[snip]
var suggest = '<div
onmouseover="javascript:suggestOver(this):" ';

Here, "javascript:" is just a label and is superfluous. In MSIE, it can
be used to switch languages, but unless VBScript has been used (and that
would be odd, on the Web), it's still unnecessary.

[snip]

Mike


[1]
<http://groups.google.co.uk/group/comp.lang.javascript/msg/fa966be4e9ba3986?hl=en>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top