After getting the first row result from JSON array
, I need to print the all result using jquery
each method.
here's my syntax
$(document).ready(function () {
$("#btnsearch").click(function() {
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function() {
$('.toprightsec').append("Title" + data.content[0].title)
.append("Intro" + data.content[0].intro_text);
});
//end show result
}, JSON);
});
The data.content[0]
is showing the first row only with looping. But the data it self not changed. How to solve the data.content[0]
.title, so the row is printed like in database?
UPDATE
After creating some tweak to my function, i have added new feature to show the result using list.
Here's the syntax
$(document).ready(function () {
//function to get the result from user-input
$("#btnsearch").click(function() {
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function(index, value) {
$("#divContent").show();
var li = $("<li><a></a><br /><p></p></li>");
$("#posting").append(li);
$("a",li).text(value.title);
$("p",li).text(value.intro_text);
});
//end show result
}, JSON);
});
The question is, how i can reset the result from if, we want to show based on another keyword, so the list is clear if user type new keywords? without refreshing the browser.
thank you.
UPDATE 2
$(document).ready(function () {
//function to get the result from user-input
$("#btnsearch").click(function() {
//clear the div
$("#divContent").html("");
valobj = $('#search_box').val();
$.getJSON("search.php", { q : valobj }, function(data,result){
//show result from database
$.each(data.content, function(index, value) {
$("#divContent").show();
var li = $("<li><a></a><br /><p></p></li>");
$("#posting").append(li);
$("a",li).text(value.title);
$("p",li).text(value.intro_text);
});
//end show result
}, JSON);
});