By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

ajax readyState 1 wall

P: n/a
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 idit 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 idgenerated 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[i] + '</div>';
ss.innerHTML += suggest;
}
}

Oct 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What's your question?




gp wrote:
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 idit 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 idgenerated 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[i] + '</div>';
ss.innerHTML += suggest;
}
}
Oct 12 '06 #2

P: n/a
gp wrote:

[snip]
var xhr;
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>
Oct 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.