I have a search engine in which I have a select
called personas
.
I would like to keep the data of select
when I click on the "Search" button
Why is not it kept and number 2 always comes out?
I guess it's because of the code:
if($i==2){
echo "<option value='$i' selected='selected'>$i</option>";
I do not know how it could be avoided and that the one we have previously indicated appears.
HTML / PHP code:
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>COMPRAR</title>
<link rel="stylesheet" href="css/estilos_reservar.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div id="contenedor">
<div class="busqueda">
<form action="<?php echo $_SERVER['PHP_SELF'];?>" name="mibusqueda" id="mibusqueda" method="POST" class="form_buscar">
<h2>COMPRAR</h2>
<label for="personas"><b>Personas:</b></label>
<?php
echo "<select name='personas'>";
for($i=1; $i<11; $i++){
if($i==2){
echo "<option value='$i' selected='selected'>$i</option>";
}else{
echo "<option value='$i'>$i</option>";
}
}
echo "</select>";
?>
<div align="center">
<input type="submit" value="Buscar" name="buscar" id="buscar"><br/>
</div>
</form>
</div>
<div class="mostrar_cabanas">
<?php
//Si pulsamos el botón "buscar"...
if(isset($_POST["buscar"])){
//aqui va el codigo necesario...
}
}
?>
</div>
</div>
</body>
</html>