0

I've searched this site for the answer that fits this scenario but couldn't.

Here's the Function which returns results from the database of all the users:

function management_active() {

include('db.php');
include('connect.php');

$result = mysqli_query($con,"SELECT * FROM $table_name WHERE activ_status='1'");

while($row = mysqli_fetch_array($result)) {

    echo '<tr>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['username'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo '<td>';
            echo '<a href="javascript:;" onclick="showAjaxModal();" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" title="" data-original-title="View Profile"><i class="fa fa-eye"></i></a>';
            echo '<a href="javascript:;" onclick="editAjaxModal();" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit User"><i class="fa fa-cogs"></i></a>';
            echo '<a href="javascript:;" onclick="delAjaxModal();" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" title="" data-original-title="Remove User"><i class="fa fa-trash"></i></a>';
        echo '</td>';
    echo '</tr>';   

    global $user_link;
    $user_link = $row['activ_key'];

}

}

This function displays all of the users that are active in a table, I then have buttons which allow a user to view the user's profiles in a modal. The modal needs to load the user's data via AJAX.

Here's the JQUERY Script at the bottom of the page:

<script type="text/javascript">
function showAjaxModal()
{
    jQuery('#show-modal').modal('show', {backdrop: 'static'});

    setTimeout(function()
    {
        jQuery.ajax({
            url: "data/ajax-profile.php",
            data: "u=<?php echo $key; ?>",
            success: function(response)
            {
                jQuery('#show-modal .modal-body').html(response);
            }
        });
    }, 800);
}
</script> 

And here's the PHP file which returns the results to the AJAX Call:

$user_link = $_GET['u'];

if ($user_link==="$user_link") {

    $result = mysqli_query($con,"SELECT * FROM $table_name WHERE activ_key='$user_link'");
    while($row = mysqli_fetch_array($result)) {

        echo $row['username'];
        echo $row['fname'];
        echo $row['lname'];

    }

} else {
    echo '
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
        </button>

        <strong>Oh Snap!</strong> Something went terribly wrong.
    </div>
    ';   
}

The issue is that I can't seem to get the correct user's details, it always outputs the very last row in the database table.

0

1 Answer 1

1

It's because you're echoing the key at the bottom, you need to pass the key while looping

echo '<a href="javascript:;" onclick="showAjaxModal(\'' . $row["activ_key"] . '\');" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" title="" data-original-title="View Profile"><i class="fa fa-eye"></i></a>';

then require the key in the JS functions

<script type="text/javascript">
function showAjaxModal(key)
{
    jQuery('#show-modal').modal('show', {backdrop: 'static'});

    setTimeout(function()
    {
        jQuery.ajax({
            url: "data/ajax-profile.php",
            data: "u="+key,
            success: function(response)
            {
                jQuery('#show-modal .modal-body').html(response);
            }
        });
    }, 800);
}
</script> 
Sign up to request clarification or add additional context in comments.

3 Comments

I would personally recommend that 1.) dont encode variables into function calls - pull the arguments from attributes on the element, 2.) dont use event handler attributes like onclick instead use jQuery to do $('selector').on('click', showAjaxModal).
While we're at it, I'd suggest a ton of other changes to the original code as well....1) don't use echo to output html blocks, 2) escape the string before using it in a query 3) use a PHP framework, 4)... :)
Thanks for the suggestions, much appreciated! I'm still learning PHP (I'm a design guy) so I indeed do have to learn all of the best practices.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.