I am using Select2 jquery plugin and I can\'t get results with json. When looking json response in browser it looks ok. Like this for example:
[{
Ok, i have your example working on my test server, please do the following
change your query to this, changed a few names for readability but should be the same functionality, important part is addition of "AS TEXT" in query
$query = $mysqli->query("SELECT id, family AS text FROM family WHERE family LIKE '%$term%'"));
while ($row = mysql_fetch_assoc($query)) {
$return[] = $row;
}
echo json_encode($return);
second, it looks like you are trying to call a property from the json response called "results"
if that was the case your json should look like this, note that family is now text due to the change above:
{
"results":
[
{
"id": "50",
"text": "Portulacaceae "
},
{
"id": "76",
"text": "Styracaceae "
},
{
"id": "137",
"text": "Dipsacaceae"
}
]
}
But your php does not create the property results, so change your results function to remove the .results property call
results: function (data) {
return { results: data };
}
final code i used (note i did not escape/sanitize the $_GET[term] or bind it to the query, recommend you do so ) if you are still having issues i can send you a link to my site example
php
/*** connection strings ***/
// get the database singleton instance
$yog = MySqlDatabase::getInstance();
// connect
try {
$yog->connect($host, $user, $password, $db_name);
}
catch (Exception $e) {
die($e->getMessage());
}
$term = $_GET['term'];
if (!$term){
$sub = $yog->query("SELECT id, family AS text FROM family");
} else {
$sub = $yog->query("SELECT id, family AS text FROM family where family like '%$term%'");
}
while ($row = mysql_fetch_assoc($sub)) {
$return[] = $row;
}
echo json_encode($return);
?>