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

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘