Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 12-21-2008, 12:42 PM AJAX Woes
pealo86's Avatar
Super Spam Talker

Posts: 849
Name: Matt Pealing
Location: England, north west
Trades: 0
Im using a tutorial I have in a book to create an AJAX live search, but one of the main functions doesn't seem to be getting called!

It appears that the line:
Code:
ajaxCallback = displayResults;
Should run the function once it is assigned using the associated code in 'ajax.js', I think? Anyway, I think that's where the code is going wrong as the function 'displayResults ()' isn't being called. It's pretty hard to tell really since no errors show up in the error console.

Any help would be great!


ajax.jx:
Code:
// AJAX library

var ajaxreq = false, ajaxCallBack;

function ajaxRequest (filename)
    {
        try
            {
                ajaxreq = new XMLHttpRequest ();
            }
        
        catch (error)
            {
                try
                    {
                        ajaxreq = new ActiveXObject ("Microsoft.XMLHTTP");
                    }
                
                catch (error)
                    {
                        return false;
                    }
            }
            
        
        ajaxreq.open ("GET", filename);
        ajaxreq.onreadystatechange = ajaxResponse;
        ajaxreq.send (null);
    }

function ajaxResponse ()
    {
        if (ajaxreq.readyState != 4)
            {
                return;
            }
        
        
        if (ajaxreq.status == 200)
            {
                if (ajaxCallBack)
                    {
                        ajaxCallBack ();
                    }
            }
        
        else
            {
                alert ('Reqeust Failed: ' + ajaxreq.statusText);
            }
        
        return true;
    }
liveSearch.js:
Code:
// AJAX Live Search

//global time variable
var time;

// start timeout with each keypress
function startSearch ()
    {
        window.clearTimeout (time);
        time = window.setTimeout ('liveSearch()', 200);
    }

// perform the search
function liveSearch ()
    {
        // assemble PHP filename
        query = document.getElementById ('searchLive').value;
        filename = 'search.php?query=' + query;
        
        // displayResults will handle AJAX response
        ajaxCallback = displayResults;
        // send AJAX request
        ajaxRequest (filename);
    }

// display search results
function displayResults ()
    {
        // remove old list
        ul = document.getElementById ('list');
        div = document.getElementById ('results');
        div.removeChild (ul);
        
        // make a new list
        ul = document.createElement ('ul');
        ul.id = 'list';
        
        names = ajaxreq.responseXML.getElementsByTagName ('name');
        
        for (i = 0; i < names.length; i ++)
            {
                li = document.createElement ('li');
                name = names[i].firstChild.nodeValue;
                text = document.createTextNode (name);
                li.appendChild (text);
                ul.appendChild (li);
            }
        
        if (names.length == 0)
            {
                li = document.createElement ('li');
                li.appendChild (document.createTextNode ('No results'));
                ul.appendChild (li);
            }
        
        // display new list
        div.appendChild (ul);
    }

var input = document.getElementById ('searchLive');
input.onkeydown = startSearch;
liveSearch.htm:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX Live Search</title>
<script type="text/javascript" src="ajax.js"></script>
</head>



<body>
<h1>Ajax Live Search</h1>
<form id="frmSearch" name="frmSearch" method="post" action="">
  <p>Search for: 
    <input type="text" name="searchLive" id="searchLive" />
  </p>
  <div class="results">
    <ul id="results">
      <li>Results will display here</li>
    </ul>
  </div>
</form>

<script type="text/javascript" src="liveSearch.js"></script>

</body>
</html>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 12-21-2008 at 12:43 PM..
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Reply     « Reply to AJAX Woes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08092 seconds with 12 queries