Php 根据数据库中下拉列表的值自动完成

Php 根据数据库中下拉列表的值自动完成,php,jquery,mysql,ajax,autocomplete,Php,Jquery,Mysql,Ajax,Autocomplete,我一周之内就要解决这个问题了。我这里有一个下拉选择,带有ajax发布值到另一个下拉列表,但现在我需要使用自动完成功能发布到文本框中。我需要的是连接我的自动完成查询和我的ajax,这样,如果我选择例如圆珠笔,所有圆珠笔将推荐自动完成。请帮我做这个。我需要完成它 这是我的密码 Ajax.php getajax.php 在这里,我在另一个下拉列表中发布值,但我不需要在文本框中发布 <?php if (isset($_POST["mainlist_id"])) { $mysqli = ne

我一周之内就要解决这个问题了。我这里有一个下拉选择,带有ajax发布值到另一个下拉列表,但现在我需要使用自动完成功能发布到文本框中。我需要的是连接我的自动完成查询和我的ajax,这样,如果我选择例如圆珠笔,所有圆珠笔将推荐自动完成。请帮我做这个。我需要完成它

这是我的密码

Ajax.php

getajax.php

在这里,我在另一个下拉列表中发布值,但我不需要在文本框中发布

<?php
if (isset($_POST["mainlist_id"])) {
    $mysqli = new mysqli("localhost", "root", "", "2015");
    $main = $mysqli->real_escape_string($_POST["mainlist_id"]);


$result1 = $mysqli->query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item");

    while($row = $result1->fetch_assoc())
    {
    ?>
    <option value ="<?php echo $row['item_code'];?>"><?php echo $row['item'];?></option>';
<?php
    }
    }
?>
autocomplete.php

<?php
    //$q=$_GET['q'];
    $mysqli = new mysqli("localhost", "root", "", "2015") or die("Database Error");
    $auto = $mysqli->real_escape_string($_GET["q"]);
    //$main = $mysqli->real_escape_string($_POST["mainlist_id"]); AND cat_code='$main'
    $sql = $mysqli->query("SELECT * FROM code WHERE item LIKE '%$auto%' GROUP BY id ORDER BY item" ); 

    if($sql)
    {
        while($row=mysqli_fetch_array($sql))
        {
            echo $row['item']."\n";
        }
    }
?>

//每当您选择标记字段时,它将获得焦点,并自动开始搜索,因此您无需键入$this.autocompletesearch查看回调焦点函数;和最小长度0。你必须发送主值并从这里获得响应

<script>
$(document).on("keyup", "#tag", function(){
    $("#tag").autocomplete({
        source: function(request, response) {
        $.getJSON("autocomplete_gethere.php", { main: $("#main").val() }, response);
        },
        minLength:0
        }).focus(function() {
        $(this).autocomplete("search", "");
    });
});
</script> 

     <script type="text/javascript">
        $('#main').change(function(){
        $.ajax({
        url : 'getajax.php',
        data :{mainlist_id : $(this).val()},
        dataType:'html',
        type:'POST',
        success:function(data){
        $('#tag').focus();    //please note this, here we're focusing in that input field
        }
        });
        });
        </script>

未经测试,如果有任何问题发表评论

运行代码时会发生什么?@Lupin当我运行代码并尝试在标记字段中输入任何内容时,您是否调试了它?尝试在AJAX成功失败时为数据变量发出警报或使用console.log。$this.autocompletesearch的用途是什么;它会在你开始打字之前显示结果,就像你说的那样。如何解决这个问题?
<script>
$(document).on("keyup", "#tag", function(){
    $("#tag").autocomplete({
        source: function(request, response) {
        $.getJSON("autocomplete_gethere.php", { main: $("#main").val() }, response);
        },
        minLength:0
        }).focus(function() {
        $(this).autocomplete("search", "");
    });
});
</script> 

     <script type="text/javascript">
        $('#main').change(function(){
        $.ajax({
        url : 'getajax.php',
        data :{mainlist_id : $(this).val()},
        dataType:'html',
        type:'POST',
        success:function(data){
        $('#tag').focus();    //please note this, here we're focusing in that input field
        }
        });
        });
        </script>