有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个回答



添加javascript函数</ p>

  $(function(){
// autocomplete
$(“#tags”)。autocomplete({
source:“search.php”,
minLength:1
});
});
</ code> </ pre> \ n

在search.php </ p>

  $ query =“SELECT标签来自YOUR_TABLE WHERE标签,如'%”。$ _ GET ['tags']。“%  'LIMIT 0,15“; 
$ rs = mysql_query($ query);
while($ row = mysql_fetch_array($ rs)){
$ tags [] = $ row ['tags'];
} \ n
echo json_encode($ tags);
</ code> </ pre>

请使用限制来加速自动提示</ p>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐