Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 22nd, 2005, 01:15 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with AJAX Function

Hello Everyone and thanks for your help in advance. I am working on an AJAX Function that quries a database then generates the results in as fashion similar to Google Suggest. The application can be located at:

http://www.kidsmedicalcare.com/Search.aspx

However, I am running into a Javascript error "Expected ')'" but I can't seem to figure out what is throwing the error. I have traced the problem somewhat to the hyperlinks that are created with the search results, i.e. if I don't make them into hyperlinks, there is no error. However, it appears, at least to my very small Javascript knowledge, that the links are formed properly (you can view the return text in the large textbox to the right), so I really don't know what the problem is. Any help on this issue will be greatly appreciated. Thanks.

Reply With Quote
  #2 (permalink)  
Old July 22nd, 2005, 01:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

can you provide some source code for your Javascript ? also try the page in Firefox as that has a better Javascript debugger and console than IE.
Reply With Quote
  #3 (permalink)  
Old July 22nd, 2005, 03:32 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the code:

// This sample only supports IE6 and FireFox

var xmlHttp;
var browser = "IE";

// Check for FireFox
if(navigator.userAgent.indexOf("Firefox") != -1)
{
   browser = "FF";
}

function CaptureSearchText()
{
    // Get a reference to the SearchTextBox control and
    // extract the contents into a variable
    var searchTextBox = document.getElementById("SearchTextBox");
    var searchText = searchTextBox.value.toLowerCase();

    //alert(searchText);

    // Call the SendSearchRequest function
    SendSearchRequest(searchText);
}

function DisplaySearchResults(searchResults)
{
    // Get a reference to the ResultsPanel control and
    // render the results from the server
    var resultsPanel = document.getElementById("ResultsPanel");

    resultsPanel.innerHTML = searchResults;

    //if(searchResults != "")
    if(searchResults != "0")
    {
        // Make sure that the panel is in the proper position
        // and that it is visible
        resultsPanel.style.top = "55px";
        resultsPanel.style.visibility = "visible";
    }
    else
    {
        // The server returned no results so hide the panel
        resultsPanel.style.visibility = "hidden";
    }
}

function DisplaySelectedResult(selectedResultText)
{
    // Get references to our controls
    var searchTextBox = document.getElementById("SearchTextBox");
    var resultLabel = document.getElementById("ResultLabel");
    var resultsPanel = document.getElementById("ResultsPanel");

    // Set the contents of the textbox and label to our selected result
    searchTextBox.value = selectedResultText;
    resultLabel.innerHTML = "You selected: " + selectedResultText;

    // Hide the results panel
    resultsPanel.style.visibility = "hidden";
}

function SendSearchRequest(searchText)
{
    // Create a new XmlHttp object
    if(browser == "IE")
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(browser == "FF")
    {
        xmlHttp = new XMLHttpRequest();
    }

    // Define the request url and encode the search text
    var requestUrl = "Server.aspx?searchtext=" + encodeURIComponent(searchText);
    //var requestUrl = requestURL = "data/test.txt";
    //document.Form1.helpbox.value = requestUrl;

    // Tell XmlHttp that our request is asynchronous
    var isAsyncRequest = true;

    // If the object is successfully created then
    if(xmlHttp)
    {
        // Set the callback function
        xmlHttp.onreadystatechange = ReceiveSearchResults;
        // Send the request
        xmlHttp.open("GET", requestUrl, isAsyncRequest);
        xmlHttp.send(null);
    }
}

function ReceiveSearchResults()
{
    // Make sure the response is complete
    if(xmlHttp.readyState == 4)
    {
        // Make sure the return status from the server is OK
        if(xmlHttp.status == 200)
        {
            // Get the results from the server and call
            // the DisplaySearchResults function
            DisplaySearchResults(xmlHttp.responseText);
            document.Form1.helpbox.value = xmlHttp.responseText;
        }
        else
        {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

Reply With Quote
  #4 (permalink)  
Old July 22nd, 2005, 04:39 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Someof the results contain apostrophes, e.g "WEGENER'S GRANULOMATOSIS" so that the links created reads
Code:
><a href="#" onclick="DisplaySelectedResult('WEGENER'S GRANULOMATOSIS')">WEGENER'S GRANULOMATOSIS</a>
The extra single quote fouls up the JavaScript
There are a few ways to fix this. The easiest, in my opinion, is to have the service return the links like:
Code:
><a href="WEGENER'S GRANULOMATOSIS" onclick="DisplaySelectedResult(this.href);return false;">WEGENER'S GRANULOMATOSIS</a>
As a side issue the lang attribute on the script element is incorrect. This refers to the natural language. You should also avoid "language" as this is now deprecated. The official way is to state the mimetype:
Code:
<script type="text/javascript" src=...

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old December 14th, 2005, 12:29 AM
Registered User
 
Join Date: Dec 2005
Location: nontaburi, , Thailand.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to tee140 Send a message via MSN to tee140
Default

I doing create chatroom by using Sajax.php.
I have problem is chatroom page not free result memory.

sorry. I can't speak english.:D

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with AJAX 2.0 ilegend .NET Framework 2.0 13 August 25th, 2008 04:27 AM
Adding AJAX to CSK2 - Problems dgrieser BOOK: Beginning ASP.NET 2.0 AJAX ISBN: 978-0-470-11283-0 0 July 29th, 2008 12:37 PM
Ajax Problems ch. 27 gigs BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 0 September 24th, 2007 05:50 AM
InStr Function - having problems mmcdonal VBScript 1 November 29th, 2004 02:50 PM
Function Display Problems??? webasp1 Classic ASP Databases 0 August 6th, 2003 03:23 PM



All times are GMT -4. The time now is 04:00 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.