Reputation: 296
I've been trying for hours to get this to work. I have a <div id=""data_friends>
tag and a hidden input field
that I want to update using AJAX. The div
tag is as follows:
<div class="friends-tab-list" id="data_friends">
<?php
//Default query limits results to 8
$sql = ("SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) > 0 LIMIT 0,8");
$query = mysqli_prepare($con, $sql);
$query->bind_param('s', $username);
$query->execute();
$result = $query->get_result();
$num_rows = $result->num_rows;
while ($row = $result->fetch_assoc()) {
$row['profile_pic'];
$row['username'];
echo "<a class='profile-img-item' href='" . $row['username'] . "'>
<img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
</a>";
}
$query->close();
?>
</div>
The hidden input is as follows: <input id='max' type='hidden' value='<?php echo $num_rows; ?>'>
I'm clicking on a View More Friends button and sending data to includes/handlers/ajax_load_profile_friends.php
using the following:
$.ajax({
url:'includes/handlers/ajax_load_profile_friends.php',
type:'POST',
dataType: 'json',
data:{'username':username, 'num_friends':num_friends, 'counter':counter},
success: function(data) {
$('#data_friends').html(data.html);
$('#max').val(data.num_rows);
}
});
The data coming from ajax_load_profile_friends.php looks like this:
$query = mysqli_prepare($con,"SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) LIMIT $counter");
$query->bind_param('s', $username);
$query->execute();
$result = $query->get_result();
$num_rows = $result->num_rows;
}
while ($row = $result->fetch_assoc()) {
$row['profile_pic'];
$row['username'];
$html = "<a class='profile-img-item' href='" . $row['username'] . "'>
<img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
</a>";
}
echo json_encode(array('num_rows' => $num_rows, 'html' => $html));
When I run this, I get a single return in my when I'm suppose to get a return of 16 records with each click I thought by doing this in my success function $('#data_friends').html(data.html);
The value in my hidden input field <input id='max' type='hidden' value='<?php echo $num_rows; ?>'>
is not updating using this $('#max').val(data.num_rows);
Is there something I'm missing in ajax_load_profile_friends.php
that is causing these behaviors?
**Keep in mind that I can get this to work when I don't use json_encode
& write success function like this $('#data_friends').html(data.html);
and remove the dataType: 'json',
from AJAX. The problem here is that in both ways, I'm not able to update my hidden input value. I figured I would try and correct this method since most examples specify json_encode()
as the way to return data.
Upvotes: 0
Views: 72
Reputation: 64
header( "Content-Type: application/json", TRUE );
$query = mysqli_prepare($con,"SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) LIMIT $counter");
$query->bind_param('s', $username);
$query->execute();
$result = $query->get_result();
$num_rows = $result->num_rows;
$html='';
while ($row = $result->fetch_assoc()) {
$html .= "<a class='profile-img-item' href='" . $row['username'] . "'>
<img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
</a>";
}
echo json_encode(array('num_rows' => $num_rows, 'html' => $html));
Upvotes: 2
Reputation: 178
You are not declaring $html variable before while loop. Try this one
<?php
$query = mysqli_prepare($con,"SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) LIMIT $counter");
$query->bind_param('s', $username);
$query->execute();
$result = $query->get_result();
$num_rows = $result->num_rows;
}
$html = '';
while ($row = $result->fetch_assoc()) {
// $row['profile_pic'];
// $row['username'];
$html .= "<a class='profile-img-item' href='" . $row['username'] . "'>
<img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
</a>";
}
echo json_encode(array('num_rows' => $num_rows, 'html' => $html));
Upvotes: 1