doumo1807831 2014-04-01 00:39
浏览 22
已采纳

显示包含在文本框中键入的句子的结果

Hello I want to make a textbox that can select data from a table, when input is givin to the textbox it should autocomplete the sentance with any words it matches within the

I already have this piece of code and it works but it only shows result of the first symbol you type in.

  1. function lookup(inputString)
  2. {
  3. if(inputString.length == 0)
  4. {
  5. $('#suggestions').hide();
  6. }
  7. else
  8. {
  9. $.post("sql_naam_klant.php", {queryString: ""+inputString+""}, function(data)
  10. {
  11. if(data.length >0)
  12. {
  13. $('#suggestions').show();
  14. $('#autoSuggestionsList').html(data);
  15. }
  16. });
  17. }
  18. }
  19. function fill(thisValue)
  20. {
  21. $('.inputString').val(thisValue);
  22. setTimeout("$('.suggestions').hide();", 200);
  23. }

HTML:

  1. <input type="text" name="naam_klant" size="20" id="naam_klant" onkeyup="lookup(this.value);" onblur="fill();" >
  2. <div class="suggestionsBox" id="suggestions" style="display: none;">
  3. <div class="suggestionList" id="autoSuggestionsList">

UPDATE:

  1. <?php
  2. $db = new mysqli('localhost', 'root' ,'*', 'records');
  3. if(!$db)
  4. {
  5. // Show error if we cannot connect.
  6. echo 'ERROR: Could not connect to the database.';
  7. }
  8. else
  9. {
  10. // Is there a posted query string?
  11. if(isset($_POST['queryString']))
  12. {
  13. $queryString = $db->real_escape_string($_POST['queryString']);
  14. // Is the string length greater than 0?
  15. if(strlen($queryString) >0)
  16. {
  17. $query = $db->query("SELECT naam_klant FROM overboekingen WHERE naam_klant LIKE '$queryString%' LIMIT 10");
  18. if($query)
  19. {
  20. while ($result = $query ->fetch_object())
  21. {
  22. echo '<li onClick="fill(\''.$result->naam_klant.'\');">'.$result->naam_klant.'</li>';
  23. }
  24. }
  25. else
  26. {
  27. echo 'ERROR: There was a problem with the query.';
  28. }
  29. }
  30. else
  31. {
  32. } // There is a queryString.
  33. }
  34. else
  35. {
  36. echo 'There should be no direct access to this naam_klant script!';
  37. }
  38. }
  39. ?>

展开全部

  • 写回答

2条回答 默认 最新

  • dongmin3754 2014-04-01 00:51
    关注

    I can't answer the question because it is incomplete. We need to see the content of the sql_naam_klant.php to see why is it like that.

    I am assuming that the error is in the query.

    Use the Wildcard Symbol (%) in your where statements.

    "Select * from tblnames where name like '%$queryString%';"
    

    Will edit this answer as soon as you provide us with complete details.

    If you want to find what you typed inside,beginning or end of a text block use the wildcard in both start and end of the search keyword. %$queryString%. If what you are looking for is the in beginning of a block of text then use $queryString% and if you're looking at the end of a text blook %$queryString.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部