Completed combobox in the edit form

Asked

Viewed 21 times

-1

how do I load this combobox filled in the edit form with the chosen value during registration?

<label for="tipo_lancamento">tipo de lancamento</lable>
<select name="tipo_lancamento">
        <option></option>
    <option value="receita">receita</option>
    <option value="despesa">despesa</option>
</select><br/>

I can load the filled inputs through the following code, but this option does not work with the combobox

<label for="pessoa">Pessoa</label><br />
<input type="text" name="pessoa" id="pessoa" value="<?=$lancamento['pessoa'];?>"  required />

  • You want to leave selected the value that comes from your select tag is this ?

  • Hello Ricardo, I would have to leave selected value that was saved in the bank during registration, however as it is an edition form, I would have come to another option, if the user wanted to change, since I appreciate the feedback

1 answer

0

Good guys, I got through a post I found on this blog http://wbruno.com.br/php/mostrar-option-de-select-escolhido-pelo-usuario-php-mysql/, I am posting the solution, because I believe that another user may have the same difficulty that I found. In my case, I understood how it works and adapted my need

1º has been declared a function, to avoid duplication of code.

<?php
function selected( $value, $selected ){
    return $value==$selected ? ' selected="selected"' : '';
}

2nd after I use it in each of my options:

<select name="tipo_lancamento">
  <option value="Despesa"<?php echo selected( 'Despesa', $lancamento['tipo_lancamento'] ); ?>>Despesa</option>
 <option value="Receita"<?php echo selected( 'Receita', $lancamento['tipo_lancamento'] ); ?>>Receita</option>
</select>

Browser other questions tagged

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