jQuery Autocomplete (Remote) - example

前端 未结 4 980
栀梦
栀梦 2020-12-16 03:49

I was really hoping to avoid posting a new question, but I cannot find a functioning example of the jQuery Autocomplete Remote feature that includes both the calling page an

4条回答
  •  长情又很酷
    2020-12-16 04:27

    Here is a trimmed version of an autocomplete I have previously used. Might have an error or two since I cut some code out.

    On the server:

        if(isset($_POST['queryString'])) {
        $queryString = $_POST['queryString'];
        $html = '';
    
        if(strlen($queryString) >1) {
            $names= explode(" ", $queryString ); 
            foreach ($names as &$value) {
                // step 1: first names
                $result= queryf("SELECT *, 
                        MATCH(productName, productTags, productCategory, productOneLine) 
                        AGAINST ('*$queryString*' IN BOOLEAN MODE) 
                        AS score FROM products
                        WHERE MATCH(productName, productTags, productCategory, productOneLine) 
                        AGAINST ('*$queryString*' IN BOOLEAN MODE)
                        AND productStatus='1'
                        ORDER BY score DESC
                        LIMIT 10") ;
                if($result) {
    while ($row = mysql_fetch_array($result)) {
                                echo '
  • '.$row['productName'].'
    '.$row['productOneLine'].'
  • '; } } } else { echo '
    • Processing...
      Please keep typing while we process your code
    '; } } } else { echo '
    • Processing...
      Please keep typing while we process your code
    '; } } else { echo 'Nothing to see here.'; }

    The script:

    function suggest(inputString){
        if(inputString.length == 0) {
            $('#suggestions').fadeOut();
        } else {
        $('#country').addClass('load');
            $.post("/autosuggest.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').fadeIn();
                    $('#suggestionsList').html(data);
                    $('#country').removeClass('load');
                }
            });
        }
    }
    function fill(thisValue) {
        $('#country').val(thisValue);
        setTimeout("$('#suggestions').fadeOut();", 600);
    }
    

    And on the XHTML page:

    The comments about "acceptance rate" are unnecessary it's more useful to post this for Google than for reputation.

提交回复
热议问题