AJAX suggest
Posted: Mon Sep 11, 2006 8:57 am
feyd | Please use
and heres the php function GetSuggestions:[/syntax]
Can anyone think why it would be giving me this error?
thanks,
-b
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm trying to use an autocomplete script from the book [u]AJAX and PHP[/u]. I've worked through some of the errors and I thought I had configured it correctly but am receiving the error, "xmlHttpGetSuggestions.responseXML has no properties." Here's the javascript xmlHttpGetSuggestion function (also, xmlHttpGetSuggestions is the XMLHttpRequest instance and contains the mysql db connection):
[syntax="javascript"]/* initiate HTTP request to retrieve suggestions for the current keyword */
function getSuggestions(keyword)
{
/* continue if keyword isn't null and the last pressed key wasn't up or
down */
if(keyword != "" && !isKeyUpDownPressed)
{
// check to see if the keyword is in the cache
isInCache = checkCache(keyword);
// if keyword is in cache...
if(isInCache == true)
{
// retrieve the results from the cache
httpRequestKeyword=keyword;
userKeyword=keyword;
// display the results in the cache
displayResults(keyword, oCache[keyword]);
}
// if the keyword isn't in cache, make an HTTP request
else
{
if(xmlHttpGetSuggestions)
{
try
{
/* if the XMLHttpRequest object isn't busy with a previous
request... */
if (xmlHttpGetSuggestions.readyState == 4 ||
xmlHttpGetSuggestions.readyState == 0)
{
httpRequestKeyword = keyword;
userKeyword = keyword;
xmlHttpGetSuggestions.open("GET",
getFunctionsUrl + encode(keyword), true);
xmlHttpGetSuggestions.onreadystatechange =
handleGettingSuggestions;
xmlHttpGetSuggestions.send(null);
}
// if the XMLHttpRequest object is busy...
else
{
// retain the keyword the user wanted
userKeyword = keyword;
// clear any previous timeouts already set
if(timeoutId != -1)
clearTimeout(timeoutId);
// try again in 0.5 seconds
timeoutId = setTimeout("getSuggestions(userKeyword);", 500);
}
}
catch(e)
{
displayError("Can't connect to server:\n" + e.toString());
}
}
}
}
}Code: Select all
// returns all PHP functions that start with $keyword
public function getSuggestions($keyword)
{
// escape the keyword string
$patterns = array('/\s+/', '/"+/', '/%+/');
$replace = array('');
$keyword = preg_replace($patterns, $replace, $keyword);
// build the SQL query that gets the matching functions from the database
if($keyword != '')
$query = 'SELECT name ' .
'FROM suggest ' .
'WHERE name LIKE "' . $keyword . '%"';
// if the keyword is empty build a SQL query that will return no results
else
$query = 'SELECT name ' .
'FROM suggest ' .
'WHERE name=""';
// execute the SQL query
$result = $this->mMysqli->query($query);
// build the XML response
$output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
$output .= '<response>';
// if we have results, loop through them and add them to the output
if($result->num_rows)
while ($row = $result->fetch_array(MYSQLI_ASSOC))
$output .= '<name>' . $row['name'] . '</name>';
// close the result stream
$result->close();
// add the final closing tag
$output .= '</response>';
// return the results
return $output;
}thanks,
-b
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]