0

我正在制作一些应用程序(管理页面),我加载了所有用户<table>,我还可以选择激活和停用每个用户。现在我的问题是我不知道如何检测按下了哪个“激活”或“停用”按钮,我的代码仅适用于列表中的第一个用户。这是我的代码:

管理页面.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; ?>

脚本.js:

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

3 回答 3

4

此代码更好地使用它:

//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";
                    }
                }
            );
    }
于 2013-08-10T12:19:08.687 回答
1

我建议将您的 ID 更改为类(因为 ID 需要是唯一的)。

如果您这样做了,我会这样做(我已将您的#optionLabelto.optionLabel#userIDto更改为.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";
                }
            }
        );
});
于 2013-08-10T12:09:41.220 回答
0

尝试这个

//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";
                    }
                }
            );
    });
于 2013-08-10T12:11:42.753 回答