Keep the record of a ComboBox in an edit form

0

I have a problem with a form to edit employees, I have a comboBox where I display the posts that exist in a select, but I can not get in this form to edit I selected the option that has the record is only printed as it would be by default. I append my code, I hope you can help me.

<?php
require_once("TipoEmpleado.php");
require_once("Empleado.php");
$elId=$_GET['id'];
$empleado = new Empleado();
$tipoEmpleado = new TipoEmpleado();
$listaTipo = $tipoEmpleado->mostrarTodos();
$imprimir=$empleado->mostrar($elId); 
?>
<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
            <title>Editar empleado</title>
            <link rel="stylesheet" href="css/bootstrap.css"/>
            <link rel="stylesheet" href="css/main.css"/>
        </head>
        <body>  
            <div>
                <form action="GuardarEditarTipoEmpleado.php" method="POST">
                    <?php foreach($imprimir as $emple){ 
                ?>
                    <div class="container">
                        <input name="txtId" type="hidden" value="<?php echo $elId; ?>" />
                        <label>Nombre</label>
                        <input type="text" name="txtNombre"  required class="form-control" id="txtNombre" value="<?php echo $emple->NomEmpleado; ?>" /><br/>
                        <label>Nombre de usuario</label>
                        <input type="text" name="txtNombreUsuario"  required class="form-control" id="txtNombreUsuario" value="<?php echo $emple->NombreUsuario; ?>" /><br/>
                        <label>Contraseña</label>
                        <input type="text" name="txtContrasena"  required class="form-control" id="txtContrasena" value="<?php echo $emple->Contrasena; ?>" /><br/>
                         <label>Correo Electronico</label>
                        <input type="text" name="txtCorreo"  required class="form-control" id="txtCorreo" value="<?php echo $emple->Correo; ?>" /><br/>
                        <label>Puesto</label>
                        <select name="txtTipo" id="txtTipo">
                        <?php
                        foreach($listaTipo as $tipo){
                        ?>
                            <option><?php echo $tipo->NomTipoEmp; ?></option>   
                        <?php
                            }   
                        ?>
                        </select>
                        <button type="btn-guardar" class="btn btn-success">Agregar</button> 
                        <a class="btn btn-danger" href="">Regresar</a>
                    </div>
                        <?php
                }
                    ?>
                </form>
            </div>
        </body>
    </html>
    
asked by Sara 12.08.2018 в 18:07
source

2 answers

0

You have to compare in for for each cycle if the option you are going to print with echo is equal to the option saved in $emple . When it is fulfilled, then print the " selected " tag in the HTML.

Another issue is that you do not have the option tag in value , so nothing is going to be sent. I do not know what is called the column that has the type_emp, but I assumed that it would be called $emple->Tipo_Emp . You replace it with the name you are using for that field. Finally, the normal thing is that the HTML options have a format like this:

<option value="ID_item">Descripcion_item</option>

in the value goes an ID and not the description.

foreach($listaTipo as $tipo){
    $selected = ($tipo->NomTipoEmp == $emple->Tipo_Emp) ? 'selected' : ''; # Operador ternario
    ?>
    <option value="<?php echo $tipo->NomTipoEmp; ?>" <?php echo $selected; ?> ><?php echo $tipo->NomTipoEmp; ?></option>   
    <?php
} 
    
answered by 12.08.2018 / 19:37
source
0

with this you solve it, since the sending of select does not send what you see, but the value="algo" , therefore you must put the visual and the value, like this:

 foreach($listaTipo as $tipo){
   ?>
   <option <?php echo "value='$tipo->NomTipoEmp'"; ?>><?php echo $tipo->NomTipoEmp; ?></option>   
   <?php
   }   
   ?>

I hope it serves you, and you have understood ... ReNiceCode ...

    
answered by 12.08.2018 в 19:23