xeditable select2远程数据获取到选择字段

时间:2016-04-16 16:23:17

标签: php jquery json ajax

我正在尝试使用我的数据库中的emote数据提取来实现xeditable插件select2,我的数据提取脚本返回json数据但是ajax调用没有检索它。我的代码如下所示: HTML     由ID

找到

的Javascript

$('#username ').editable({
  type: 'select2',  
  url: '../payments/person.php',    
  pk: 1,    
  onblur: 'submit',
  emptytext: 'None',
     select2: {
                placeholder: 'Select a Requester',
                allowClear: true,
                width: '230px',
                minimumInputLength: 3,
                id: function (e) {
                    return e.person_id;
                },
                ajax: {
                    url: '../payments/person.php',
                    dataType: 'json',
                    data: function (term, page) {
                        return { query: term };
                    },
                    results: function (data, page) {
                        return { results: data };
                    }
                },
                formatResult: function (employee) {
                    return employee.name;
                },
                formatSelection: function (employee) {
                    return employee.name;
                },
                initSelection: function (element, callback) {
                    return $.get('../payments/person.php', { query: element.val() }, function (data) {
                        callback(data);
                    }, 'json'); //added dataType
                }
            }
});

Php代码以json格式返回数据,如下所示:

[{"id":"1","name":"AdminAdmin"}]

我确信我的ajax代码没有正确收集数据,当我点击它时它也不会加载弹出窗口。如果有人可以提供帮助。

我的用于获取的php代码如下所示:

   if (isset($_POST['person_id'])) {
   $id = trim($_POST['person_id']);
     $result = array();
   $id = mysqli_real_escape_string($con,$id);
   $res = mysqli_query($con,"SELECT person_id,Concat(first_name ,last_name) as name FROM k_people ");
while ($row = mysqli_fetch_array($res)) {

$result[] = array(
  'id' => $row['person_id'],
  'text' => $row['name']
);

}
echo json_encode($result);

}

1 个答案:

答案 0 :(得分:0)

我认为[{"id":"1","name":"AdminAdmin"}]应为[{"id":"1","text":"AdminAdmin"}]