Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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
Javascript Jquery发送带有Json的数组_Javascript_Php_Jquery_Function_Autocomplete - Fatal编程技术网

Javascript Jquery发送带有Json的数组

Javascript Jquery发送带有Json的数组,javascript,php,jquery,function,autocomplete,Javascript,Php,Jquery,Function,Autocomplete,我在这段代码中使用jquery ui: $(document).ready(function () { $(function() { function log( message ) { $( "<div>" ).text( message ).prependTo( "#log" ); $( "#log" ).scrollTop( 0 ); } }); $( "#birds" ).a

我在这段代码中使用jquery ui:

$(document).ready(function () {
    $(function() {
        function log( message ) {
            $( "<div>" ).text( message ).prependTo( "#log" );
            $( "#log" ).scrollTop( 0 );
        }
    });
    $( "#birds" ).autocomplete({
        source: "search.php",
        minLength: 4,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });
});



<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds">
</div>
    <div class="ui-widget" style="margin-top:2em; font-family:Arial">
    Result:
    <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content">
    </div>
/div>
但当我输入:OEM=>时,数组中的每个选项都会出现!为什么? 只有当我输入一些时,它才会出现。 那么我在search.php中的错误在哪里呢

这一个工作正常:


你好

问题是,无论用户类型如何,都返回相同的数组。在PHP代码中,您应该做的是根据用户给定的值执行一些检查。 请注意,jQuery代码只列出PHP代码返回的内容,因此筛选仍由您决定

要按照您在评论中提到的方式创建数组,您需要如下操作:

$array = array(
array("id"=>"the id", "label"=>"the label", "value"=>"the value"),
array("id"=>"another id", "label"=>"another label", "value"=>"another value"),
);

调用echo json_encode$array后,您将得到所需的结果。

根据TGO的说法,您必须在PHP代码中进行过滤。jQuery将对您为源选项提供的URL发出GET请求。因此,在PHP代码中,应该使用$_GET['term']的值来决定回显哪个JSON数组。PHP有测试一个字符串是否是另一个字符串的子字符串的函数,您可以使用这样的函数来过滤数组

$(document).ready(function () {
        function log( message ) {
            $( "<div>" ).text( message ).prependTo( "#log" );
            $( "#log" ).scrollTop( 0 );
        }
    $( "#birds" ).autocomplete({
        source: function( request, response ) {
          $.getJSON( "search.php", {
            term: request.term
          }, response );
        },
        minLength: 4,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });
});

当你输入原始设备制造商在哪里?您正在尝试将json从php发送到jquery,或者以其他方式发送?当我在输入字段(发送到search.php)中键入OEM时,您是否看到jquery链接?thx,那么我如何创建这样的数组[{id:Botaurus stellaris,label:Great-rawin,value:Great-rawin},{id:Tachybaptus ruficollis,label:Little-Grebe,value:Little-Grebe}]因为这似乎是正确的数组,应该返回它您需要索引数组。
$(document).ready(function () {
        function log( message ) {
            $( "<div>" ).text( message ).prependTo( "#log" );
            $( "#log" ).scrollTop( 0 );
        }
    $( "#birds" ).autocomplete({
        source: function( request, response ) {
          $.getJSON( "search.php", {
            term: request.term
          }, response );
        },
        minLength: 4,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });
});
<?php
$found_terms = array();
if(isset($_GET['term'])){
    $term = trim($_GET['term']); 
    $myarray = array(
        "somelabelvalue",
        "somelabelvalue1",
        "somelabelvalue2",
        "somelabelvalue3"
        );
    foreach($myarray as $value){
        if(strstr($value, $term)){
            $found_terms[] = $value;
        }
    }
}
echo json_encode($found_terms);
?>