Why is not the selected data kept when clicking search?

0

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>
    
asked by omaza1990 03.01.2018 в 20:46
source

1 answer

1
  

Why is not it kept and number 2 always comes out?

The answer is because you are always comparing if $i is equal to 2 , instead of validating if $i equals the data sought.

Solution:

Since the data of form is sent per POST and the name of select is personas , then it is to be expected that if a search was performed the variable $_POST['personas'] must exist and it will have the value selected by the user.

Your code could be the following:

<select name="personas">
<?php

// Si la variable no existe, establecemos el valor por defecto
if (!isset($_POST['personas'])) {
    $_POST['personas'] = 2;
}

for ($i=1; $i<11; $i++) {

    if ($i == $_POST['personas']) {
        echo "<option value='$i' selected='selected'>$i</option>";
    } else {
        echo "<option value='$i'>$i</option>";
    }
} ?>
</select>
    
answered by 03.01.2018 / 21:12
source