John Davidson

How to hide an option in a dropdown menu PHP?

0 comments
Message:


I am learning PHP, and would like to know if there is any way to hide an option depending on what I get,
basiccaly this is my code it's simple.


<?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>

Basically I would like to know if when $ _GET = ['state'] = 'Nuevo', when I press the button (name = 'update') what I receive from the POST, it only shows me an option (En Ejecución), in theory there are 3 states , Nuevo, En Ejecución and Cerrado. When the status is ¿Nuevo' I don't want it to show me the option "EN Ejecucuion" and when the option received is "EN Ejecucuion" I do not want it to show me the option 'Nuevo'.



My Answer:
Yes, you can hide an option in a dropdown menu in PHP by using conditional statements to determine when to display the option. Here is an example of how you can achieve this:

php



In this example, the option with the value "option2" will only be displayed if the variable `$showOption` is set to true. You can replace the condition with your own logic to determine when to hide or show the option.

Remember to adjust the condition based on your specific requirements.

Rate this post

3 of 5 based on 6258 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us