Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用jquery自动完成消息应用程序的用户名?_Php_Jquery_Mysql_Jquery Ui - Fatal编程技术网

Php 如何使用jquery自动完成消息应用程序的用户名?

Php 如何使用jquery自动完成消息应用程序的用户名?,php,jquery,mysql,jquery-ui,Php,Jquery,Mysql,Jquery Ui,我正在玩一个小消息框,并尝试在jquery UI中使用自动填充功能,但它不起作用。没有自动填充的内容。这是我到目前为止得到的 composemessage.php <script> $(function() { $( "#tags" ).autocomplete({ source: "usersarray.php", dataType:'json', minLength: 0, delay:0 }); $( "#tags" ).click(fu

我正在玩一个小消息框,并尝试在jquery UI中使用自动填充功能,但它不起作用。没有自动填充的内容。这是我到目前为止得到的

composemessage.php

<script> 

$(function() {
$( "#tags" ).autocomplete({
    source: "usersarray.php",
    dataType:'json',
    minLength: 0,
    delay:0

});

$( "#tags" ).click(function() {
    $( "#tags" ).autocomplete("search","");
});
});
</script> 
$id = $_SESSION['account_id'];

$getmsg = "SELECT * FROM user WHERE account_id = $id";      
$showmsg = @mysqli_query ($dbc, $getmsg);
        while ($row = mysqli_fetch_array($showmsg, MYSQLI_ASSOC)) {

$arrResults = array($row['user_username']);


} // END WHILE


// Print them out, one per line
echo json_encode($arrResults);

JSON结果应该如下所示:

users = ['user1', 'user2'];

根据您的jquery请求,您希望使用这种格式。您能告诉我们usersarray.php文件的输出是什么吗?如果用户名对您很重要,不要忘记屏蔽它们。

Json必须采用以下格式:

 [{label: 'label1', value: 'value1'},{label: 'label2', value: 'value2'},{label: 'label3', value: 'value3'}];
javascrip部分:

$( "#tags" ).autocomplete({
    source: function( request, response ) {
        var term = request.term;

         $.ajax({
            url: "username.php",
            dataType: 'json',
            async: true,
            data: request,
            timeout: 3000,
            success: function(data,status,xhr){ 
                response( data );
            }
        });

    },
    delay: 100,
    minLength: 0
});
HTML

<input name="username" class="detailedViewTextBox suggest_hidden" id="tags" autocomplete='off' value="" type="text" size="18">


@用户是否发出ajax请求?您是否尝试直接导航到usersarray.php以查看它是否正在打印您期望的用户名?我有-问题在于我如何获得该数组,因为它根本没有打印出该数组。是的,当我不是从mysql查询中手动输入数组时,ajax请求是有效的。如果在php文件中使用数组而不是db查询时,autocompletes起作用,试着调试你的php代码。谢谢-有什么建议吗?好的-那么我该如何格式化我的php/mysql查询以将其转换为json格式呢?我选择这种方式调用ajax,以便能够设置异步方式和超时。你的查询可以是这样的,然后关联数组完成任务:$sql=“选择用户名作为值,用户名作为用户的标签,其中用户名如“$term%”限制为10”划掉该标记-抱歉,它显示为“空”“那么我想在这一点上,你需要让你的查询工作。使用
选择用户名…
,而不是
选择*。
。选择字段名时,最好更明确。验证数据库中该表中是否有记录。确保对
$row['fieldName']
的调用与
选择字段名
的调用相同,无论是什么。我想从这里开始。同时打印您的
$id
,并确保您在那里有一个期望值。