Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
86
rated 0 times [  92] [ 6]  / answers: 1 / hits: 8903  / 9 Years ago, mon, april 27, 2015, 12:00:00

This scripts are used,
https://www.devbridge.com/sourcery/components/jquery-autocomplete/



I am using jQuery Autocomplete for searching users from my DB.



Below is controller which returns Json:



public function searchusers1() {
if ($_GET) {
$query = $this -> input -> get('query');

$searcharray = $this -> model_usermanage -> searchuser($query);

$a_json = array();
$a_json_row = array();
foreach($searcharray as $row) {
//$user_firstname = htmlentities(stripslashes($row['user_firstname']));
//$user_lastname = htmlentities(stripslashes($row['user_lastname']));
$user_email = htmlentities(stripslashes($row['user_email']));
//$a_json_row[user_firstname] = $user_firstname;
$a_json_row[user_email] = $user_email;

array_push($a_json, $a_json_row);
}
echo json_encode($a_json);
}
}


Below is my jQuery:



$('#reply_bcc').autocomplete({
serviceUrl: '<?php echo base_url(); ?>index.php/hi/test/searchusers1',
minChars: 3,
onSelect: function (suggestion) {
console.log('You selected: ' + suggestion.data + ', ' + suggestion.data);
}
});


Here is my JSON response:



[{user_email:[email protected]},{user_email:[email protected]},{user_email:[email protected]},]


My HTML:



<div class=col-md-10>
<input type=text name=reply_bcc id=reply_bcc autocomplete=off class=form-control>
</div>


My PROBLEM is:



I am getting below error in console and I can not see this searched values as dropdown in my html input:




Uncaught TypeError: Cannot read property 'length' of undefined




What's going wrong with this?



Thanks!


More From » jquery

 Answers
7

The documentation explains Response from the server must be JSON formatted following JavaScript object:



{
// Query is not required as of version 1.2.5
query: Unit,
suggestions: [
{ value: United Arab Emirates, data: AE },
{ value: United Kingdom, data: UK },
{ value: United States, data: US }
]
}




And you are just returning an array without the format value/data. You can change your format in PHP or use the transformResult function to create the suggestions property with your array.



You should add an object in your PHP code:



$obj = new stdClass();
$obj->suggestions = $a_json;
echo json_encode($obj);

[#37601] Sunday, April 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisw

Total Points: 625
Total Questions: 92
Total Answers: 88

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
ellisw questions
Mon, Aug 23, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 21, 20, 00:00, 4 Years ago
;