Lost Password?


  #1 (permalink)  
Old 09-23-2007, 11:16 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,428
Last Blog:
Google Web Toolkit
Credits: 1,208
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default AJAX: Suggest

One AJAX tool popularized by Google is Google Suggest, and all that is required is a basic modification to the code in the previous tutorial. Lets assume we have a php file that connects to a database that contains all the names of the countries. We are also assuming that the php script is setup to receive a GET variable named $country.

Lets start with the basic AJAX code derived from the previous tutorial, but I'm going to change a few variable names and allow it to accept a parameter.

JavaScript Code:
  1. function suggest($var)
  2.   {
  3.   var xmlHttp;
  4.   try
  5.     {
  6.     xmlHttp=new XMLHttpRequest();
  7.     }
  8.   catch (e)
  9.     {
  10.     try
  11.       {
  12.       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  13.       }
  14.     catch (e)
  15.       {
  16.       try
  17.         {
  18.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  19.         }
  20.       catch (e)
  21.         {
  22.         alert("Ajax is not supported by your browser.");
  23.         return false;
  24.         }
  25.       }
  26.     }
  27.      xmlHttp.onreadystatechange=function()
  28.       {
  29.       if(xmlHttp.readyState==4)
  30.         {
  31.         document.suggestform.suggestCountry.value=xmlHttp.responseText;
  32.         }
  33.       }
  34.     xmlHttp.open("GET", "countries.php", true);
  35.     xmlHttp.send(null);
  36.   }

Lets assume we have a form that looks like this:

HTML4Strict Code:
  1. <form name="suggestform">
  2. Country: <input type="text" name="country" />
  3. Suggested Country: <input type="text" name="suggestCountry" />
  4. </form>

What we are going to do, is take the value in the "country" input box and submit it to countries.php which will query the database and return the countries related to the name we typed thus far.

JavaScript Code:
  1. function suggest($var)
  2.   {
  3.   var xmlHttp;
  4.   try
  5.     {
  6.     xmlHttp=new XMLHttpRequest();
  7.     }
  8.   catch (e)
  9.     {
  10.     try
  11.       {
  12.       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  13.       }
  14.     catch (e)
  15.       {
  16.       try
  17.         {
  18.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  19.         }
  20.       catch (e)
  21.         {
  22.         alert("Ajax is not supported by your browser.");
  23.         return false;
  24.         }
  25.       }
  26.     }
  27.      xmlHttp.onreadystatechange=function()
  28.       {
  29.       if(xmlHttp.readyState==4)
  30.         {
  31.         document.suggestform.suggestCountry.value=xmlHttp.responseText;
  32.         }
  33.       }
  34.     var url = "countries.php?country=" + $var
  35.     xmlHttp.open("GET", url, true);
  36.     xmlHttp.send(null);
  37.   }

And thus, we will change our html form to this:

HTML4Strict]<form name="suggestform">
Country: <input type="text" name="country" onkeyUp="suggest(this.value)" />
Suggested Country: <input type="text" name="suggestCountry" />
</form>[/code]

The final code will look like this:

[highlight="HTML4Strict Code:
    <html> <head> <title>Ajax Suggest</title> <script type="text/javascript"> function suggest($var)   {   var xmlHttp;   try     {     xmlHttp=new XMLHttpRequest();     }   catch (e)     {     try       {       xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");       }     catch (e)       {       try         {         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");         }       catch (e)         {         alert("Ajax is not supported by your browser.");         return false;         }       }     }      xmlHttp.onreadystatechange=function()       {       if(xmlHttp.readyState==4)         {         document.suggestform.suggestCountry.value=xmlHttp.responseText;         }       }     var url = "countries.php?country=" + $var     xmlHttp.open("GET", url, true);     xmlHttp.send(null);   } </script> </head> <body> <form name="suggestform"> Country: <input type="text" name="country" onKeyUp="suggest(this.value)" /> Suggested Country: <input type="text" name="suggestCountry" /> </form> </html>
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall

Last edited by John; 09-23-2007 at 11:20 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX: Intro John Tutorials 1 11-07-2007 09:52 PM
Considering Ajax, Part 1: Cut through the hype Ronin JavaScript and CSS 0 12-13-2006 03:36 PM
Myth-Busting AJAX (In)security Jordan AJAX 1 12-03-2006 02:21 PM
AJAX Evolved Jordan JavaScript and CSS 0 09-09-2006 02:12 PM
More AJAX Resources NeedHelp JavaScript and CSS 2 07-13-2006 01:57 PM


All times are GMT -5. The time now is 05:04 AM.

Contest Stats

WingedPanther ........ 2630.54
Xav ........ 2576.41
Brandon W ........ 1697.27
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 959.05
dcs ........ 646.09
Steve.L ........ 475.59
orjan ........ 407.96
chili5 ........ 378.6

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads