Set OPTION as SELECTED based on database values

Asked

Viewed 55,209 times

9

I am preparing a form where I am setting the values of the fields with the database data.

I’m having trouble setting the chosen option as selected to the <select/> in the code below:

PHP

<!-- ATENCAO -->
<label>Tipo Beneficiario: </label> <span> <? echo $TipoBeneficiario; ?> </span>
<div class="">
  <select name="tipoBeneficiario">
    <option value="0">Selecione</option>
    <option value="1">Conjugue</option>
    <option value="2">Filho</option>
    <option value="3">Mãe/Pai</option>
    <option value="4">Compannheira(o)</option>
  </select>
</div>

Screenshot of the form

Tela de alteração de dados

2 answers

16


An option too, would be to test the value for each option and print selected when corresponding, example:

<label>Tipo Beneficiario: </label> <span> <?php echo $TipoBeneficiario; ?></span>
<div class="">
  <select name="tipoBeneficiario">
    <option value="0" <?=($TipoBeneficiario == 'Selecione')?'selected':''?> >Selecione</option>
    <option value="1" <?=($TipoBeneficiario == 'Conjugue')?'selected':''?> >Conjugue</option>
    <option value="2" <?=($TipoBeneficiario == 'Filho')?'selected':''?> >Filho</option>
    <option value="3" <?=($TipoBeneficiario == 'Mãe/Pai')? 'selected':''?> >Mãe/Pai</option>
    <option value="4" <?=($TipoBeneficiario == 'Companheira(o)')?'selected':''?> >Compannheira(o)</option>
  </select>
</div>

In the example, the condition is tested using if ternary, where

 <?=($TipoBeneficiario == 'Companheira(o)')? 'selected' : ''?>

is the same as

<?php 
if($TipoBeneficiario == 'Companheira(o)'){
   echo 'selected';
}
?>

If you do not have the short tags option enabled on the server or prefer not to use, you can do:

<label>Tipo Beneficiario: </label> <span> <?php echo $TipoBeneficiario; ?></span>
<div class="">
  <select name="tipoBeneficiario">
    <option value="0" <?php echo $TipoBeneficiario=='Selecione'?'selected':'';?> >Selecione</option>
    <option value="1" <?php echo $TipoBeneficiario=='Conjugue'?'selected':'';?> >Conjugue</option>
    <option value="2" <?php echo $TipoBeneficiario=='Filho'?'selected':'';?> >Filho</option>
    <option value="3" <?php echo $TipoBeneficiario=='Mãe/Pai'?'selected':'';?> >Mãe/Pai</option>
    <option value="4" <?php echo $TipoBeneficiario=='Companheira(o)'?'selected':'';?> >Companheira(o)</option>
  </select>
</div>  

10

Assuming the value of the checkbox is contained in the variable $TipoBeneficiario, so that you can mark a selection box option as selected you should compare each of the options with the value present in the variable.

In your case, it looks like you are saving the selected caption instead of the selection value. So you can make the comparison as follows:

Check which one is selected

// inicializar variáveis a vazio
$check0 = $check1 = $check2 = $check3 = $check4 = "";

/* verificar qual o valor contido na variável $TipoBeneficiario
 * e preencher a variável de marcação com o atributo "selected"
 */
switch ($TipoBeneficiario) {
  case "Selecione": {
    $check0 = "selected";
    break;
  }
  case "Conjugue": {
    $check1 = "selected";
    break;
  }
  case "Filho": {
    $check2 = "selected";
    break;
  }
  case "Mãe/Pai": {
    $check3 = "selected";
    break;
  }
  case "Compannheira(o)": {
    $check4 = "selected";
    break;
  }
}

Output

echo '
<label>Tipo Beneficiario:  </label>  <span> '.$TipoBeneficiario.'</span>
<div class="">
  <select name="tipoBeneficiario">
    <option value="0" '.$check0.'>Selecione</option>
    <option value="1" '.$check1.'>Conjugue</option>
    <option value="2" '.$check2.'>Filho</option>
    <option value="3" '.$check3.'>Mãe/Pai</option>
    <option value="4" '.$check4.'>Compannheira(o)</option>
  </select>
</div>';

Browser other questions tagged

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