0

I'm making some app (admin page), I loaded all users in <table>, also I have option to activate and deactivate each user. Now my problem is I don't know how to detect which 'activate' or 'deactivate' button is pressed, my code only works for first user in list. This is my code:

adminPage.php:

<?php
$getData = $mysqli->query("select * from login");
while($row = $getData->fetch_assoc()):
?>
<tr id="dataRows">
    <td id="firstTd"><?php echo $row['user_id']; ?></td>
    <td><?php echo $row['username']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['lastname']; ?></td>
    <td><?php echo $row['active']; ?></td>
<?php if($row['active'] == 1){ ?>
    <td><label id="userID" hidden><?php echo $row['user_id']; ?></label>
    <label id="userActive" hidden><?php echo $row['active']; ?></label>
    <label id="optionLabel"><?php echo 'Deactivate'; ?></label></td>

<?php } else{ ?>
    <td><label id="userID" hidden><?php echo $row['user_id']; ?></label>
    <label id="userActive" hidden><?php echo $row['active']; ?></label>
    <label id="optionLabel"><?php echo 'Activate'; ?></label></td>

<?php } endwhile; ?>

script.js:

$("#optionLabel").click(function(){
            $.post("option.php", {"id" : $("#userID").html(), "com" : $("#userActive").html()},
                function(data){
                    if(data == "Updated"){
                        window.location.href = "adminPage.php";
                    }
                }
            );
    });
2
  • 1
    You can't have multiple ID's. Commented Aug 10, 2013 at 12:06
  • Well, I have more then one "optionLabel" buttons. My code only works when I click on first one. Since all buttons have same atributes I need to detect on which one I press so I can deactivate and activate apropriate user. Commented Aug 10, 2013 at 12:11

3 Answers 3

1

I would suggest to change your ID's to classes (because ID's need to be unique).

If you have done that I would do this (I have changed your #optionLabel to .optionLabel and #userID to .userID:

$(".optionLabel").click(function(){
        $.post("option.php", {"id" : $(this).prev('.userID').html(), "com" : $("#userActive").html()},
            function(data){
                if(data == "Updated"){
                    window.location.href = "adminPage.php";
                }
            }
        );
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

//adminPage.php

  $getData = $mysqli->query("select * from login");
                while($row = $getData->fetch_assoc()):
        ?>
        <tr id="dataRows">

                <td id="firstTd"><?php echo $row['user_id']; ?></td>
                <td><?php echo $row['username']; ?></td>
                <td><?php echo $row['email']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['lastname']; ?></td>
                <td><?php echo $row['active']; ?></td>
                <?php if($row['active'] == 1){ ?>

                            <td><label class="userID" hidden><?php echo $row['user_id']; ?></label>
                            <label class="userActive" hidden><?php echo $row['active']; ?></label>
                            <label class="optionLabel"><?php echo 'Deactivate'; ?></label></td>
                        <?php } else{ ?>
                            <td><label class="userID" hidden><?php echo $row['user_id']; ?></label>
                            <label class="userActive" hidden><?php echo $row['active']; ?></label>
                            <label class="optionLabel"><?php echo 'Activate'; ?></label></td>

            <?php } endwhile; ?>

//script.js

$(".optionLabel").click(function(){
            $.post("option.php", {"id" : $(this).siblings('.userID').html(), "com" : $(this).siblings('.userActive').html()},
                function(data){
                    if(data == "Updated"){
                        window.location.href = "adminPage.php";
                    }
                }
            );
    });

6 Comments

Perfect :) Thank you. And also I would like to know why my quesition id downvoted. If I can't find solution for my problem it's natural to ask help from someone else.
I'm not quite sure. I'm new to stackoverflow as well :)
I know, thank you. I was addressing to that person who downvoted my question.
Happy to help, if you could accept my solution that would be awesome :D
@Alen Hi. I did NOT downvote you (just for the record). To somewhat answer your question as to why your question was downvoted could be, that you did not show/mention what it is that you have tried, or the effort you put in to fix it yourself; "the proof is in the pudding", as it were, and many think that you could be asking a question just to get some (free) code. Since you are new to StackOverflow, and welcome by the way, you should read the "help" section. Many answers can be found in there, that may help you in the future.
|
0

this code work better use this:

//adminPage.php

<?php
                $getData = $mysqli->query("select * from login");
                while($row = $getData->fetch_assoc()):
        ?>
        <tr id="dataRows">

                <td id="firstTd"><?php echo $row['user_id']; ?></td>
                <td><?php echo $row['username']; ?></td>
                <td><?php echo $row['email']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['lastname']; ?></td>
                <td><?php echo $row['active']; ?></td>
                <?php if($row['active'] == 1){ ?>

                            <td><label id="optionLabel" onclick="updatestatus(<?php echo $row['user_id']; ?>, 1);">Deactivate</label></td>
                        <?php } else{ ?>
                            <td>
                            <label id="optionLabel" onclick="updatestatus(<?php echo $row['user_id']; ?>, 0);">Activate</label></td>

            <?php } endwhile; ?>

//script.js

    function updatestatus(id , status){
var ac_id = id;
var ac_st = status;
            $.post("option.php", {"id" : ac_id, "com" : ac_st},
                function(data){
                    if(data == "Updated"){
                        window.location.href = "adminPage.php";
                    }
                }
            );
    }

1 Comment

Hidden Element Are Removed and Statics value like 'active' and 'deactive' not printing

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.