0

I have the following hidden input field with a dynamic value.

<input id='max' type='hidden' value='<?php echo $num_rows; ?>'>

I'm wanting to update that value based on $num_rows that are returned from a ajax_load_profile_friends.php page.

Here is what I have so far:

    $("#viewAllFriends").click(function(){
        counter = counter+16;

            $.ajax({

                url:'includes/handlers/ajax_load_profile_friends.php',
                type:'POST',
                data:{'username':username, 'num_friends':num_friends, 'counter':counter},

                    success: function(data) {
                        $('#data_friends').html(data);
                        $('#max').val("??");
                            }

                    });
            });

With each successive click of View More Friends button, I get a new value for $num_rows. Being new to Ajax I'm not sure how to return it to the success function and then update the value in the hidden input.

$num_rows returns like this $num_rows = $result->num_rows; in ajax_load_profile_friends.php.

This is part of a larger problem I was having earlier but have narrowed it down to this and tried to simplify my question. Any help, examples, links would be super!

1
  • so what is your problem ? Commented Feb 26, 2019 at 2:41

1 Answer 1

1

Change the PHP so it returns JSON. So instead of

echo $html;

it should do:

echo json_encode(array('num_rows' => $num_rows, 'html' => $html));

Then in the JavaScript you can extract both return values:

        $.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);
            }

        });
Sign up to request clarification or add additional context in comments.

19 Comments

hmmm...The button goes dead with this script and I get some console errors.
Is #viewAllFriends inside #data_friends?
If you're overwriting any elements that have event bindings, see stackoverflow.com/questions/203198/…
no #viewAllFriends is just below the #data_friends div .
Then I can't think of any reason why the button would go dead. Unless there's a syntax error that's causing this function not to run at all.
|

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.