单击mysqli中的另一个按钮后显示按钮

时间:2017-05-12 19:41:06

标签: php button mysqli

我在index.php中有这段代码:

<?php require 'class.php';require_once './session.php';$conn = new db_class();$read = $conn->read();$data = [];while($fetch = $read->fetch_array()){ 
$data[] = $fetch;}?>
<table   class = "table table-bordered table-responsive ">
        <thead>
            <tr>
                <th>Segment</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($data as $fetch): ?>
                <tr>
        <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']?>">
                    <td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
                    <td class="text-center">
                        <button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update"> 
                            <span class = "glyphicon glyphicon-edit"></span> Update
                        </button> 
                        | 

                        <button class = "btn btn-success action-btn" data-id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
                            <span class = "glyphicon glyphicon-check"></span> Activate
                        </button> 
                        | 
                        <button class = "btn btn-danger action-btn" data-id="<?= $fetch['id'] ?>"  data-action="deactivate">
                            <span class = "glyphicon glyphicon-folder-close"></span> deactivate
                        </button>
                    </td>
                </tr>
            <?php endforeach; ?>

        </tbody>
    </table>

这是class.php中的函数

  public function read()
{
    $stmt = $this->conn->prepare("SELECT id,segment FROM `segments`") or die($this->conn->error);
    if ($stmt->execute()) {
        $result = $stmt->get_result();
        return $result;
    }

}

我想要的是显示是否禁用激活以仅显示激活按钮,如果启用激活则仅显示取消激活按钮, 我使用0表示停用,1表示激活

1 个答案:

答案 0 :(得分:0)

首先,您需要隐藏停用按钮,使用style display隐藏 按钮。在激活按钮上写下点击事件,然后使用jquery hide/show隐藏激活按钮&amp;显示停用按钮。

$(".activate-btn").click(function() {
$(this).hide();
$(".deactivate-btn").show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table   class = "table table-bordered table-responsive ">
        <thead>
            <tr>
                <th>Segment</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($data as $fetch): ?>
                <tr>
        <input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id']?>">
                    <td class="segment" contenteditable="true"><?= $fetch['segment'] ?></td>
                    <td class="text-center">
                        <button class = "btn btn-default action-btn" data-id="<?= $fetch['id'] ?>" data-action="update"> 
                            <span class = "glyphicon glyphicon-edit"></span> Update
                        </button> 
                        | 

                        <button class = "btn btn-success activate-btn action-btn" data-id="<?= $fetch['id'] ?>" type="submit" data-action="activate">
                            <span class = "glyphicon glyphicon-check"></span> Activate
                        </button> 
                        
                        <button style="display:none" class = "btn btn-danger deactivate-btn action-btn " data-id="<?= $fetch['id'] ?>"  data-action="deactivate">
                            <span class = "glyphicon glyphicon-folder-close"></span> deactivate
                        </button>
                    </td>
                </tr>
            <?php endforeach; ?>

        </tbody>
    </table>