加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Select2 ajax没有显示结果

发布时间:2020-12-13 16:59:08 所属栏目:PHP教程 来源:网络整理
导读:我正在使用select2和ajax来查询我的数据库中某些分类法下的术语,但是当我搜索搜索框时,只是挂起“搜索”而不检索任何结果. 这是我的HTML select multiple="" name="regions1[]" id="regions1" class="job-manager-multiselect select2-hidden-accessible" re
我正在使用select2和ajax来查询我的数据库中某些分类法下的术语,但是当我搜索搜索框时,只是挂起“搜索”而不检索任何结果.

这是我的HTML

<select multiple="" name="regions1[]" id="regions1" class="job-manager-multiselect select2-hidden-accessible" required="" tabindex="-1" aria-hidden="true"></select>

我的jquery:

<script>
jQuery(function($) {
$(document).ready(function() {
$( "#regions1" ).select2({        
ajax: {
    url: "/ajax/connect.php",dataType: 'json',delay: 250,data: function (params) {
        return {
            q: params.term // search term
        };
    },processResults: function (data) {
        // parse the results into the format expected by Select2.
        // since we are using custom formatting functions we do not need to
        // alter the remote JSON data
        return {
            results: data
        };
    },cache: true
},minimumInputLength: 2
  });
  });
   });
 </script>

和我的PHP代码查询数据库,我希望获得分类法“job_listing_region”下的所有术语名称

<?php

  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";

   try {
$conn = new PDO("mysql:host=$servername;dbname=mydatabase",$username,$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
   }
    catch(PDOException $e)
   {
   echo "Connection failed: " . $e->getMessage();
   }



  // strip tags may not be the best method for your project to apply extra 
   layer of security but fits needs for this tutorial 
   $search = strip_tags(trim($_GET['q'])); 

 // Do Prepared Query
   $query = $conn->prepare("
   SELECT * FROM (
    SELECT wp_terms.name
   FROM wp_terms
   JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_id = wp_terms.term_id
    WHERE taxonomy = 'job_listing_region'
    AND count = 0
    ) as T"
     );

    // Add a wildcard search to the search variable
     $query->execute(array(':search'=>"%".$search."%"));


   // Do a quick fetchall on the results
    $list = $query->fetchall(PDO::FETCH_ASSOC);

   // Make sure we have a result
   if(count($list) > 0){
    foreach ($list as $key => $value) {
    $data[] = array('id' => $value['name'],'text' => $value['name']);              
   } 
    } else {
   $data[] = array('id' => '0','text' => 'No Products Found');
   }


// return the result in json
echo json_encode($data);

正如您所看到的,我正在检索我的数据,但搜索只是挂起.

enter image description here

提前致谢.

解决方法

在这里找到解决方案 How to load JSON data to use it with select2 plugin

需要像这样重新创建我的结果

processResults: function (data) {
return {
    results: $.map(data,function(obj) {
        return { id: obj.id,text: obj.text };
    })
};
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读