简体   繁体   中英

How do I do a Quicksearch or autocomplete on a selectbox populated from a database?

I have a select box populated a from a database and would like to do a quicksearch or a autocomplete to search the elements inside the select box. Below is my code:

    <?
           print("<table>");
        print("<tr>");
            print("<td>");
            //print("<div id='searchable-form' >");  //
            print("<div id='searchable'>Search: <input type='text' id='searchFILE' size='33' autocomplete='off' /></div>");
            print("<select name='lktyp' id='lktypSearch' size='10' >");
            //Create new SQL object
            $db = new NSC_SQL($db2config);
            $db->from($bllcust1,"BCUST#,BNAME");
            $db->order("BCUST#");
            $db->select();
            $results = $db->fetchAssoc();
            $db->dspSQL();
            foreach($results as $row)
            {
                $bcust = trim($row['BCUST#']);
                $bname = trim($row['BNAME']);
                print("<option value='$bcust'>$bcust ~ $bname</option>");
            }
            print("</select>");
            print("</div>");
            print("</td>");
        print("</tr>");
        print("<tr>");
            print("<td colspan='2' align='center'>");
            //print("<input type='submit' name='search' value='Search'>");
            print("<input type='button' value='Select' onclick='lkp();'>");
            print("<input type='button' name='cancel' value='Cancel' onClick='parent.$.fancybox.close();'>");
            print("</td>");
        print("</tr>");
    print("</table>");

?>

The simplest way would be to implement Jquery autocomplete . I won't do the code for you but if you find it difficult create a new question posting some of the code you tried and someone will help.

Two nice alternatives to jQuery autocomplete which was already mentioned are Chosen and Select2 . Both of these require jQuery or another supported JS framework. jQuery autocomplete and Select2 have great support for remote datasets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM