Log in

View Full Version : Jquery w/ json search



crobinson42
07-05-2013, 06:58 PM
I'm having a difficult time with my script, i'm newer to js also. The problem i'm having is the script will execute once just fine, however if you attempt to start the search over, it hangs.


$(document).ready(function(){
$('#keyword').bind('keyup',function(){
var keyword = $('#keyword').val();
var limit = $('#limit').val();

if(keyword.length>3){
searchKeyword();
}
else if(keyword.length==0){
$('#searchResult').html('');
searchTerm='';
}
});

$('#search').click(searchKeyword);

});

var searchKeyword = function(){
var resultDiv =$('#searchResult');
var searchTerm = $('#keyword').val();
var resultLimit = $('#limit').val();

resultDiv.html('<img src="/img/loading.gif">');

$.getJSON('/json/reports/master_report_search.php?limit='+resultLimit+'&keyword='+searchTerm,function(data){

resultDiv.html('');

resultDiv.append(data.length+' results for "'+searchTerm+'"');

//filter through results
for (var i=0; i<data.length; i++){

var row = '<div><a href="/reports/master/view.php?id='+data[i].id+'"> '+data[i].name + ' '+ data[i].date+'</a></div>';

resultDiv.append(row);


}

});
}

jscheuer1
07-05-2013, 07:27 PM
I might have to see the:

/json/reports/master_report_search.php

file, but I'm thinking the first result might be cached. To prevent that do:


$.ajaxSetup({
cache: false
});

at the beginning, before the:


$(document).ready(function(){
$('#keyword').bind('keyup',f . . .

The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out. Oh, and I'd probably need to see that PHP file I mentioned as well, its code, as I cannot see that unless you provide it or a link to where you got it from.

crobinson42
07-05-2013, 07:57 PM
$.ajaxSetup({ cache: false });

Did the trick!! Thank you :-)