Reputation: 83
I want to get all of array value with ajax which that is coming from MySQL. I can't get all of result. I can get only 1 result.
My JQuery codes are:
$("input.input-search").keyup(function(){
var name = $(this).val();
if(name !='')
{
$.ajax({
type: 'post',
url: 'ajax.php?bol=search',
data: {'name':name},
dataType: 'json',
success: function(val)
{
x = val.length;
for (i = 1; i<=x; i++){
$(".search-result").html(val[i].user+' * '+x);
}
},
error: function(name){
$(".search-result").html("Nəticə yoxdur...");
}
});
}
});
PHP Codes are:
case "search":
$name = trim($_POST['name']);
$q = mysql_query("SELECT * FROM `users` WHERE `user` LIKE '%".$name."%' ORDER by id;");
if(mysql_affected_rows() > 0){
while($arr = mysql_fetch_array($q)){
$array[] = $arr;
}
echo json_encode($array);
}
break;
Upvotes: 1
Views: 99
Reputation: 3476
It's simple. You are overwriting your elements HTML content every time your loop runs. With the jQuery
.html("...")
method you set a new content for your selected element every time your loop runs.
So use
.append('...')
instead.
Of course at the very beginning of your success - method empty your element with
.html("");
Upvotes: 5
Reputation: 1143
If your query is only returning 2 rows, the problem lies in your Javascript for
loop. You're starting at 1 when the array index starts at 0. Try this:
for (i = 0; i <= x; i++) {...}
Upvotes: 4