jQuery / PHP - 检测按下哪个按钮

时间:2013-08-10 12:03:13

标签: php jquery

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

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

3 个答案:

答案 0 :(得分: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";
                    }
                }
            );
    }

答案 1 :(得分:1)

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

如果您这样做了,我会这样做(我已将您的#optionLabel更改为.optionLabel并将#userID更改为.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";
                }
            }
        );
});

答案 2 :(得分: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";
                    }
                }
            );
    });