如何在 PHP 下拉菜单中隐藏选项?

时间:2021-03-24 12:53:43

标签: php

我正在学习 PHP,想知道是否有任何方法可以根据我得到的内容隐藏选项, 基本上这是我的代码,很简单。

<?php 
require 'database.php';
session_start();

if(!isset($_POST['update'])){
    $id=$_GET['id'];
    $state=$_GET['state'];
    $sql="SELECT * from states";
    $show=$conn->prepare($sql); 
    $show->execute();
    $result = $show->fetchAll(PDO::FETCH_ASSOC);
    
}else{
    $id=$_POST['id'];
    $state=$_POST['state']; 
    $sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
    $update=$conn->prepare($sql); 
    $update->bindParam(':id',$_POST['id']);
    $update->bindParam(':fk_state',$_POST['state']);
    $update->execute();
}
?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <title>Document</title>
    </head>
    <body>
    <body>
     <div class="ml-5"><?php require 'partials/header.php' ?></div>
        <div class="container col-5">
            <h1 class="text-center mb-5">Extreme Techonologics</h1>
            <h2 class="text-center">Ingrese los campos a cambiar</h2>
            <form action="pqr_update.php" method="POST">
                <div class="form-group">
                    <input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
                </div>
                <div class="form-group my-5">
                    <label for="pqr_type">Seleccione el estado del PQR:</label>
                    <select class="form-control" id="state" name="state">
                    <?php foreach($result as $state):?>   
                    <option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
                    <?php endforeach ?>             
                    </select>
                </div>
                <button type="submit" class="btn btn-primary" name="update">Enviar</button>
            </form>
        </div>
        <a href="logout.php">Cerrar Sesión</a>
    </body>
    </html>
</html>

基本上我想知道当 $ _GET = ['state'] = 'Nuevo' 时,当我按下 button (name = 'update') 时我从 POST 收到的信息,理论上它只向我显示一个选项 (En Ejecución)有 3 个州,Nuevo、En Ejecución 和 Cerrado。当状态为 ¿Nuevo' 时,我不希望它显示选项“EN Ejecucuion”,当收到的选项为“EN Ejecucuion”时,我不希望它显示选项“Nuevo”。

1 个答案:

答案 0 :(得分:0)

根据上面的要求,试试这个:

<?php 
require 'database.php';
session_start();

if(!isset($_POST['update'])){
    $id=$_GET['id'];
    $state=$_GET['state'];
    $sql="SELECT * from states";
    $show=$conn->prepare($sql); 
    $show->execute();
    $result = $show->fetchAll(PDO::FETCH_ASSOC);
    
}else{
    $id=$_POST['id'];
    $state=$_POST['state']; 
    $sql="UPDATE pqrs SET fk_state=:fk_state WHERE idPqrs=:id";
    $update=$conn->prepare($sql); 
    $update->bindParam(':id',$_POST['id']);
    $update->bindParam(':fk_state',$_POST['state']);
    $update->execute();
}
?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <title>Document</title>
    </head>
    <body>
    <body>
     <div class="ml-5"><?php require 'partials/header.php' ?></div>
        <div class="container col-5">
            <h1 class="text-center mb-5">Extreme Techonologics</h1>
            <h2 class="text-center">Ingrese los campos a cambiar</h2>
            <form action="pqr_update.php" method="POST">
                <div class="form-group">
                    <input type="hidden" value="<?php echo $id?>" class="form-control" name="id">
                </div>
                <div class="form-group my-5">
                    <label for="pqr_type">Seleccione el estado del PQR:</label>
                    <select class="form-control" id="state" name="state">
                    <?php
                        foreach($result as $state):
                            if (
                                  ($_REQUEST['state'] == '¿Nuevo\'' && $state['state'] != 'EN Ejecucuion')
                                  &&
                                  ($_REQUEST['state'] == 'EN Ejecucuion' && $state['state'] != '¿Nuevo\'')
                            ):
                    ?>   
                        <option value="<?php echo $state['idState'];?>"><?php echo $state['state'];?></option>
                    <?php endif; endforeach; ?>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary" name="update">Enviar</button>
            </form>
        </div>
        <a href="logout.php">Cerrar Sesión</a>
    </body>
    </html>
</html>