I’m trying to get json code from page.I use this
private function __ajax_admin_search($username = '')
{
$result = $this->admin_login->Admin_Username_Ajax($username);
$count = count($result);
for ($i = 0;$i < $count;$i++)
$arr[$i] = $result[$i]->username;
echo (json_encode($arr));
}
And try to show it by jQuery and JavaScript
$("#AdminSearch").bind("change keyup", function() {
var url = "http://localhost/PmMusic/index.php/admin/ajax/admin_search/"+$("#AdminSearch").val();
$.getJSON(url,function(data){
if (data.length == 0) // or arr == null
{
$("#AutoSearch").hide(1000);
}
else
{
$("#AutoSearch").show(1000);
}
});
});
Now my problem is,I can’t detect when __ajax_admin_search doesn’t find any result and first if (in JavaScript) doesn’t be TRUE(always it’s FALSE).
how Can I detect in JavaScript when my php code doesn’t find any result in database.
Make sure to initialise
$arr = Array();before yourforloop.Then, the PHP script will return
[]as the JSON object, which you can detect in JavaScript without problems.