www.webdeveloper.com
Results 1 to 1 of 1

Thread: JQuery Autocomplete - multiple terms?

Threaded View

  1. #1
    Join Date
    Apr 2011
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    33

    JQuery Autocomplete - multiple terms?

    DanInMA suggested I start a new thread

    I've got a fine looking search box with a JQuery autocomplete that looks impressive at first glance.

    I'm trying to make it work for multiple terms --- "foobar" works fine, but the script stops matching and suggesting (in the browser) after a space, so "foobar baz" won't be matched. Watching in Firebug, I see the serviceURL is still providing matching data, so I'm pretty sure the problem is located somewhere on the Javascript side. I've spent more time than I should reading similar threads in places like StackOverflow and have a thread over at PHPBuilder as well, but the last advice there was that I should use PHP's json_encode() ... and that's not the point ... AAMOF it's not been working with json_encode.

    Anyway, here's the stuff:

    In the HTML header of the search page:

    Code:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="includes/jquery.autocomplete.js"></script>
    I've attached the latter *js file.

    In the page itself:
    Code:
    <script type="text/javascript">
        var a = $('#searchbox').autocomplete({
        serviceUrl:'autocomplete.php',
        minChars:3,
        multiple: true,
        multipleSeparator: " ",
        delimiter: /(,|;)\s*/, // regex or character
        maxHeight:400,
        width:300,
        zIndex: 9999,
        deferRequestBy: 0, //miliseconds
        noCache: false //default is false, set to true to disable caching
        //callback function:
        // local autosugest options:
        //local lookup values
    });
    </script>
    Here's Autocomplete.php:
    PHP Code:
    <?php

    // autocomplete.php --- feeds suggestions to
    // the AJAX autocomplete (Java)script on our searchbox.

    /* According to the docs for the JQuery plugin, we must
       echo the output EXACTLY as shown (a modified JSON).
       Third list ("data") is optional.
    {
     query:'Li',
     suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
     data:['LR','LY','LI','LT']
    }
    */

    // connect to the DB
    include "includes/functions/db.func.php";
    $connect=dbConn();
    if (!
    $connect) die("DB connection failed");

    if(isset(
    $_GET['query']) && $_GET['query'] != '') {
       
    $jsonout=array();
       
    $qr=($_GET['query']);
       
    $qr=urlencode(mysql_escape_string($qr));
       
    $dbquery="select title from our_table where live=1 and title like '%$qr%' limit 15";
       
    $result mysql_query($dbquery);
       if (!
    $result) exit;
       
    $replacer=array("\n","\r\n","<br>","<br />","<BR>");
       while (
    $row mysql_fetch_row($result)) {
          
    $jsonout[] = str_replace($replacer," ",urldecode($row[0]));
       }
       
       
    // return the array as json with PHP
       
    echo "{
        query:'"
    .$qr."',
        suggestions:["
    ;
       
       
    $counter=1$count=count($jsonout);
       foreach (
    $jsonout as $jj) {
          echo 
    "'".$jj."'";
          if (
    $counter<$count) {
             echo 
    ",";
          }
          
    $counter++;
       }
       echo 
    "]
       }
       "
    ;
    }
    // logging routine omitted
    ?>
    Thanks very much, in advance, for any hints or clue-stick clobbers.
    Attached Files Attached Files
    Last edited by dalecosp; 05-09-2012 at 11:50 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles