phpnewbie phpnewbie - 7 months ago 158
Javascript Question

xeditable select2 remote data fetching into a select field

I am trying to implement xeditable plugin select2 with emote data fetch from my database , my data fetch script is returning the json data but the ajax call is not retrieving it . my code looks like the following:
HTML
found by 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 code returns the data in json format like below:

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


I am sure my ajax code is not collecting the data properly , and when i click on it it doesnt load the popup as well. if someone could help.

my php code for fetching look like the following:

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);


}

Answer

I think [{"id":"1","name":"AdminAdmin"}] should be [{"id":"1","text":"AdminAdmin"}].