php - JSON data do not appear in jquery ui autocomplete -
i using jquery ui autocomplete display filtered data database. using browser's network utility confirm json data returned server correctly. nothing displayed in autocomplete menu , looking @ page's source "no search results". if use response body browser's network utility source jquery ui autocomplete, works fine. appreciated.
the html
<div class="col-lg-4 col-sm-4 col-xs-12 pull-right ui-widget"> <div class="input-group"> <input class="form-control" name="afm" id="autocomplete" /> <span class="input-group-addon"><i class="fa fa-search"></i></span> </div> </div>
the javascript
$( "#autocomplete" ).autocomplete({ delay:500, minlength:2, source: "getafmjson.php", select: function( event, ui ) { location.href="userpage.php?idx=22&q="+ui.item.id; } });
the php
<?php if (!defined('dp_base_dir')) { require_once("base.php"); } // prevent direct access $isajax = isset($_server['http_x_requested_with']) , strtolower($_server['http_x_requested_with']) === 'xmlhttprequest'; if(!$isajax) { $user_error = 'access denied - not ajax request...'; trigger_error($user_error, e_user_error); } require_once(dp_base_dir."/models/config.php"); // user typed in autocomplete input $term = sanitize(trim($_get['term'])); $a_json = array(); $a_json_row = array(); $a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "only letters , digits permitted...")); $json_invalid = json_encode($a_json_invalid); // allow space, unicode letter , digit, underscore , dash if(!preg_match('/^[0-9]*$/', $term) || $term == 0) { print $json_invalid; exit; } global $db; $sql="select `company_afm`, `company_id` `data_companies` `company_afm` '".$db->sql_escape($term)."%'"; $result = $db->sql_query($sql); while($row = $db->sql_fetchrow($result)) { $a_json_row["label"] = $row['company_afm']; $a_json_row["id"] = $row['company_id']; array_push($a_json, $a_json_row); } $json = json_encode($a_json); header('content-type: application/json'); print $json; ?>
when typing "998" in autocomplete input json response in browser's network utility like: [{"label":"998916950","value":"53"}]
please help!!
after searching lot, found out problem had way editors encode scripts in utf-8. in fact, when edited scripts codeanywhere, editor allocated byte order mark (bom) @ beginning of scripts. when json generated scripts or embedded in scripts encoded in utf-8 bom, contained bom hidden characters, rendered invalid , hence useless jqueryui autocomplete. used notepad++ convert json generating script , container scripts utf-8 without bom not cleaning everything. luckily, found emrahgunduz/bomcleaner, checked scripts included in application , cleaned them bom characters.
Comments
Post a Comment