dps57553 2013-09-16 17:36
浏览 71

有jQuery函数使用来自外部php文件的数据和mySQL查询

Right now I have a site that has a dropdown menu, and in that menu contains about 1200 locations of different places. What I want to do is replace that dropdown menu with a textbox and when the user starts typing in the name of the location, it will populate the list accordingly. I have been able to do this successfully using an array that I create in the actual script block of my code, but what I want to do is not replace that with the data that comes from a MySQL query from an external php file. This is what I have working using the array.

Here is the php associated with this question:

<?php
include('common.php');
$sql = "SELECT LoginUserName FROM LoginUser";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
    $users = $row['LoginUserName'];
//echo $hour.":".$minutes;
}
// $timeHour = $_POST['hours']; This isn't needed.
echo $users;
$content = $_POST['users']; //get posted data
?>

Here is what I currently have working using the array hard coded

<script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];

    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

And then the html used on the page is

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>

So now what I am trying to do is to to use AJAX to call the php script which has the MySQL query and return what it gets and in turn populate the search results to have the same effect. Here is what I have come up with, keep in mind I am still new to the jQuery / AJAX thing...

<script>
  $(function() {
    var availableTags = [
      $.ajax({
       type: "POST",
       url: "getme.php",
       data: { content: content
       }
      })
    ];

    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

Any help is greatly appreciated. Thank you very much in advance

  • 写回答

1条回答 默认 最新

  • duan1989643 2013-09-16 18:12
    关注

    Add a javascript function

    $(function() {
       //autocomplete
       $("#tags").autocomplete({
        source: "search.php",
            minLength: 1
       });              
    });
    

    And in search.php

    $query = "SELECT tags FROM YOUR_TABLE WHERE tags like '%".$_GET['tags']."%' LIMIT 0,15";
    $rs = mysql_query($query);
    while ($row = mysql_fetch_array($rs)) {
        $tags[] = $row['tags'];
    }
    
    echo json_encode($tags);
    

    Please use limit to speed up the auto suggestion

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏