Set Default Value HTML form with PHP

Asked

Viewed 760 times

0

I have the following situation: I have a simple example of a form to display a bank’s information to change and then record it again. In this case the problem is in NOME_OPERADOR, where I wanted to already have set the value that is written in the bank, but to offer in a Selct the options in case you want to change the operator responsible for a given 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 the WHILE to take the table other operators, brings me all operators (As I wish), however I wanted to know the possibility of first (selected) the operator already recorded in the bank related to this activity. That would be more or less the doubt (Sorry if it got a little confused). NOTE: I have a OPERATORS TABLE, and a ACTIVITIES TABLE, where only the OPERATOR COD is stored.

inserir a descrição da imagem aqui

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

1 answer

1


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

<?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();
  • I adjusted but returned the same results, all OPERATORS. While I have echo in this structure: echo "<option value="$id">COD: $id&nbsp;&nbsp;&nbsp;&nbsp;PROJETO: $nome&nbsp;&nbsp;&nbsp;CLIENT: $email</option> n"; I can adapt the printf to echo?

  • When you give a blank page, add these two lines at the beginning of the script, ini_set('display_errors', true); error_reporting(E_ALL); what is the name of the id field of $reoperador ? @Tiagoib

  • The field name is COD_OPERADOR. It would be the name in the OPERATORS TABLE?

  • Would that be to include the Selected tag? <select Selected="Selected" class="form-control" name="operator name" id="operator name" class="textbox" value="<? php echo $data["NOME_OPERADOR"]; ?>">

  • @Tiagoib saw the edition?

  • I changed the code of the question according to what you suggested, take a look. @rray

  • Which edition you refer to?

  • I had put the name of the wrong variable, the right one was $dados. @Tiagoib.

  • I adjusted again the Code of the above question. However, it continues to bring only operators. It’s like I haven’t been getting the tag: Selected="Selected". @rray

  • Worked, needed to reload previous page. @rray

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.