• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

AJAX using PHP ...

As promised in my previous post, a little more on AJAX, using PHP to do the action. What I’ ll explain in this post: I’ ll type something in a text- field, and AJAX will search for possible answers in my database while I’ m typing. Compare it with the search- function in Facebook.

How do you start?
Create a basic HTML- page, with an inputfield and a div- container where your hints will be displayed. It should look like this:
<form name="testForm">
Zoekwaarde: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" />
</form>

Hints: <div id="searchHints"></div>

Now we’ ll write our Javascript code. Javascript you say? Yes Javascript: AJAX, Asynchronious Javascript + XML. The function doWork() is going to get the text you type in the searchfield. Once you get the text, you have to “talk” to a php- page, which will send back its result. Next you’ ll need to catch the result and put it in the searchHints- div.

First doWork():
function getHTTPObject(){
        if (window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
        } else if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
        } else {
                alert("Your browser does not support AJAX.");
                return null;
        }
}

function doWork(){
        httpObject = getHTTPObject();
        if (httpObject != null) {
                httpObject.open("GET", "searchHints.php?inputText=" + document.getElementById(‘inputText’).value, true);
                httpObject.send(null);
                httpObject.onreadystatechange = setOutput;
        }
}

as you can see, the php- file which is going to do my work is called searchHints.php and will echo the answers. SearchHints.php knows what to search for by giving it a querystring with the info needed. In this case the inputText of the searchfield. This is how searchHints.php looks like:
// DVDModel contains the function which is going to return the info from my database
$dvdMod = new DVDModel;
      
if (isset($_GET[‘inputText’])) {
        $searchHints = $dvdMod->returnSearchHints($_GET[‘inputText’]);
              
        foreach($searchHints as $searchHint) {
                echo "<div class=’searchHint_item’>";
                echo "<a href=’film.php?url=$searchHint->IMDB_Link‘>";
                echo "<b>" . $_GET[‘inputText’] . "</b>";
                echo substr($searchHint->name, strlen($_GET[‘inputText’]), strlen($searchHint->name));
                echo "</a>";
                echo "</div>";
        }
}

OK, so this php- file echo’ s the results. We’ ll need to catch this when everything is ready to be published. Well, we declared it in our previous function doWork():
httpObject.onreadystatechange = setOutput;

So setOutput will be called when searchHints.php returned its results. setOutput has to display the info on our page:
function setOutput(){
        if(httpObject.readyState == 4){
                document.getElementById(’searchHints’).innerHTML = httpObject.responseText;
        }
}

Now use CSS to adjust the display. If you followed the steps correctly, it should work like the Facebook- search you probably use every day.

 Original Source:
http://blog.samvanfleteren.be/?p=451

AddThis Social Bookmark Button

Posted at 10:50:16 am | Permalink | Posted in PHP  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange