0

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 divtag 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);
View all friends button

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.

1
  • When you use json_encode you get the num_rows null but the html is correct? Commented Feb 27, 2019 at 9:56

2 Answers 2

2
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));
Sign up to request clarification or add additional context in comments.

15 Comments

When I concatenate $html as suggested the button does nothing. No errors thrown or errors in console, just a button that does nothing. When I remove .= and replace with = I get a single record returned.
what are getting when you run this console.log(data) ?
Without .= I'm getting on first click num_rows: 24, html: "<a class='profile-img-item' href='la_fway'>…elion2.jpg' title='la_fway'> on second click I'm getting same thing, only num_rows=40. So it is counting up, but only returning a single record. When I use .= I get nothing.
Are you getting your result from ajax like you wanted?
I'm really running out of ideas about what could be going wrong? Can I get access to your web page?
|
1

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));

3 Comments

this way does something. I'm getting to 40 records returning, but it won't display all 43. The value in my hidden input field is still not updating. hmm...weird.
@Raylene What is the value of counter?
The counter begins at 8 - the initial query num_rows for hidden input - value='<?php echo $num_rows; ?>' I have it increasing with this: $("#viewAllFriends").click(function(){ counter = counter+16; I echoed this on the page and it works. I have this in the ajax page to evaluate for the final query: if($counter > $num_friends || $counter == $num_friends){ $counter = "No More Friends to Show"; When I use the json_encode method however, it stops at 40 (there are 43 records). My bootstrap alert doesn't show, b/c my if statement nvr evaluates to true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.