Set Default Value HTML Form with PHP

0

I have the following situation: I have a simple example form to display information for a bank to change and then write them back. In this case the problem is in the NAME_OPERATOR, where I wanted to have already set the value that is stored in the database, but that offers in a selct the options in case you want to change the operator responsible for a certain task. As the code below, if I take the WHILE excerpt, it is bringing right the operator recorded in the bank, but when I put WHILE to get the other operators from the table, it brings me all the operators (As I wish), but I wanted to know possibility that in the first (selected) the operator recorded in the bank would already be referring to this activity. It would be more or less the doubt (I'm sorry if it was a bit confusing). NOTE: I have a TABLE OPERATORS, and TABLE ACTIVITIES, where only the OPERATOR CODE is stored.

$resultado=mysql_query("select * from atividades 
                                    INNER JOIN operadores on cod_operador=codoperador_atividade
                                    where COD_ATIVIDADE = $id"); 
$dados     = mysql_fetch_array($resultado);

$reoperador = mysql_query("SELECT * FROM operadores c WHERE c.codfuncao_operador=2 order by nome_operador");

    <TRECHO DO FORM HTML>
     <select selected="selected" class="form-control" name="nome_operador" id="nome_operador" class="textBox"  value="<?php echo $dados["NOME_OPERADOR"]; ?>">  
                            <?php
                             while($l = mysql_fetch_array($reoperador)) {
                                $id     = $l["COD_OPERADOR"];
                                $nome   = $l["NOME_OPERADOR"];
                                $email  = $l["EMAIL_OPERADOR"];

                                //faz a comparação do operador selecionado com demais itens da lista             
                                $selected = ($id == $dados['COD_OPERADOR']) ? 'selected="selected"' : '';

                                //escreve a option
                                printf('<option value="%d" %s>NOME:%s - E-MAIL: %s</option>',$id, $selected,  $nome, $email);
                                                        }
                            mysql_close();
                            ?>

                        </select>
    
asked by anonymous 17.09.2015 / 15:15

1 answer

1

To leave a selected option it is necessary to compare the registry id that will be updated with the list of options (operator) and add in the <select> tag the selected="selected" attribute.

<?php
    while($l = mysql_fetch_array($reoperador)) {
        $id     = $l["COD_OPERADOR"];
        $nome   = $l["NOME_OPERADOR"];
        $email  = $l["EMAIL_OPERADOR"];

        //faz a comparação do operador selecionado com demais itens da lista             
        $selected = ($id == $dados['cod_operador']) ? 'selected="selected"' : '';

        //escreve a option
        printf('<option value="%d" %s>NOME:%s - E-MAIL: %s</option>',$id, $selected,  $nome, $email);
    }
    mysql_close();
    
17.09.2015 / 15:24