SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Auto complete textbox from database

    Hi all,
    I have texbox where I want some function that will on keyup will display similar names from database on the fly. If anyone has any idea about this,please let me know. I have googled it but not found appropriate code. Can anyone tell me what keyword should I use?
    Thanks

  2. #2
    SitePoint Enthusiast Cborrow's Avatar
    Join Date
    Apr 2005
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your looking for something like this?

    http://ajaxian.com/archives/using-aj...-auto-complete

    You can find more by searching for something like "auto complete textbox ajax php"

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You mean like google suggest?

    I wrote something simialr recently.

    search.php

    PHP Code:
    function ajaxsearch($input) {
     
     
        
    $search addslashes($input);
        
        
    //Get every page title for the site.
        
    $sql mysql_query("
            SELECT * FROM 
                hotel_pages 
            where 
                page_name like('" 
    $search "%')
            ORDER BY page_name"
    ) or die(mysql_error());
        
        if(
    mysql_num_rows($sql) == 0) {
            echo 
    'No results';
        } else {
            while(
    $rows mysql_fetch_assoc($sql)) {
                
                echo 
    $rows['page_name'] .' 'substr(strip_tags(stripslashes($rows['page_text'])), 0100) .'<br />';
            }
        }
    }

    ajaxsearch($_GET['search']); 
    form.php
    PHP Code:
    <script type="text/javascript">
    function 
    createRequestObject() { 
         
        var 
    req
        if(
    window.XMLHttpRequest){ 
            
    // Firefox, Safari, Opera... 
            
    req = new XMLHttpRequest(); 
        } else if(
    window.ActiveXObject) { 
            
    // Internet Explorer 5+ 
            
    req = new ActiveXObject("Microsoft.XMLHTTP"); 
        } else { 
            
    // There is an error creating the object, 
            // just as an old browser is being used. 
            
    alert('There was a problem creating the XMLHttpRequest object'); 
        } 
        return 
    req

         
        
    // Make the XMLHttpRequest object 
    var http createRequestObject(); 
         
    function 
    searchSuggest(act) {       
        
    // Open script for requests 
        
    http.open('get''search.php?search=' +act); 
        
    http.onreadystatechange handleResponse
        
    http.send(null); 

         
    function 
    handleResponse() { 
        if(
    http.readyState == && http.status == 200){ 
            var 
    response http.responseText
            if(
    response) { 
                
    // UPDATE ajaxTest content 
                
    document.getElementById("updateMe").innerHTML response; ; 
            }  
        } 


    function 
    update(answer) { 
        
    searchSuggest(answer); 
    }
    </script>

    <form id="frmSearch" action="search.php">
        <input type="text" id="search" name="search" alt="Search Criteria" 
            onkeyup="searchSuggest(this.value);" autocomplete="off" />
        <input type="submit" id="cmdSearch" name="cmdSearch" value="Search" alt="Run Search" />
    </form>


    <div id="updateMe"></div> 
    As a basic bare bones script, play with it
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks. yes,just like google suggest. Can I display it inside the textbox itself? Thanks again.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yes you could display it in the textbox but that would cause you all sorts of problems if more than one result is returned!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How to handle that by the way? Thanks Again.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Have a look at http://www.dynamicajax.com/fr/AJAX_S...0_312_314.html which is where I based my script from. It has the dropdown attatched to the search box as well as some other javascripts for handling the selection etc.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out Scriptaculous.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  9. #9
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is some debate about this, but because the query to the database needs to be as quick as possible, It's best to get just the field you are looking for, rather than using "*".

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yes I agree Hammer, also consider the fact that a query is made EVERY time you type a letter.
    It was suggested in another thread that one query is done and the results dumped into an array and THAT array is used for the searching.

    For a small database, either solution would work but for a massive database I really wouldnt use either!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did one of these once, but limited the action to every three characters to keep the activity down.

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quite a good comprimise Hammer
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •