Set OPTION as SELECTED based on database values

9

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

I'm having trouble setting the option chosen as selected to <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>

Form Screen Shot

    
asked by anonymous 17.06.2014 / 18:17

2 answers

12

An option would also test the value for each option and print selected when matching, eg:

<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 option of short tags 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>  
    
01.07.2014 / 22:56
9

Assuming that the value of the selection box is contained in the variable $TipoBeneficiario , so that you can select a certain option from the selection box as selected you must compare each option with the value present in the variable.

For your case, it looks like you're saving the chosen caption instead of the selection value. So you can do the comparison as follows:

Check which 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;
  }
}

Make 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>';
    
17.06.2014 / 18:37