jQuery select2返回json


                    

您好,在这种情况下,使用 jquery select2-plugin </ em> </ p>:

  $(document).ready(function(){
     $('#dropdown_users')。select2({
         占位符:“搜索用户”,
         minimumInputLength:2
         倍数:假,
         ajax:{
             网址:“ search.php”,
             dataType:'json',
             quietMillis:100,
             数据:功能(术语,页面){
                 返回{
                     q:学期
                     page_limit:10
                 };
             },
             结果:函数(数据,页面){
                 返回{
                     结果:数据
                 };
             }
         }
     });
});


</ code> </ pre>

和此 search.php </ em>:</ p>

 包括“ storescripts / connect_to_mysql.php”;

$ data = trim(strip_tags($ _ GET ['data']));

if(!empty($ data)&& isset($ data)&& strlen($ data)!= 0){
     $ stmt = $ dbh-> prepare('从表中选择产品,在产品中喜欢:keyword');
     $ keyword =“%”。$ data。“%”;
     $ stmt-> bindParam(':keyword',$ keyword,PDO :: PARAM_STR);
     $ stmt-> execute();
     而($ user = $ stmt-> fetch(PDO :: FETCH_ASSOC)){
         $ user ['data'] = htmlentities(stripslashes($ user ['product']));
         $ user_set [] = $ user;
     }
     回声json_encode($ user_set);
}其他{
     $ user_set [] = array(“ result” => 0,“ text” =>“您忘了输入!”);
}
回声json_encode($ user_set);
</ code> </ pre>

每次我进入控制台时:</ p>

  [{“结果”:0,“文本”:“您忘记输入了!”}]
</ code> </ pre>

即使我键入两个以上的字符,我也会在控制台中收到此消息。
任何人都可以帮助解决这个问题? 问候!</ p>
     </ div>

展开原文

原文

Hi there using the jquery select2-plugin in this context:

$(document).ready(function () {
    $('#dropdown_users').select2({ 
        placeholder: "Search User", 
        minimumInputLength: 2, 
        multiple: false, 
        ajax: { 
            url: "search.php", 
            dataType: 'json', 
            quietMillis: 100, 
            data: function (term, page) {
                return {
                    q: term, 
                    page_limit: 10 
                };
            },
            results: function (data, page) {
                return {
                    results: data
                };
            }
        }
    });
});

<input type="hidden" id="dropdown_users" />

and this search.php:

include "storescripts/connect_to_mysql.php";

$data = trim(strip_tags($_GET['data']));

if(!empty($data) && isset($data) && strlen($data) != 0) {
    $stmt = $dbh->prepare('SELECT product FROM `table` WHERE `product` LIKE :keyword');
    $keyword = "%".$data."%";
    $stmt->bindParam(':keyword', $keyword, PDO::PARAM_STR);
    $stmt->execute();   
    while ($user = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $user['data']=htmlentities(stripslashes($user['product']));
        $user_set[] = $user;
    }
    echo json_encode($user_set);
} else {                      
    $user_set[] = array("result" => 0 , "text" => "You forgot to type!");
}
echo json_encode($user_set);  

Every time i get in my console:

[{"result":0,"text":"You forgot to type!"}]

Even if I type more than two characters I get this message in my console. Anybody could help with this issue? Greetings!!

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