assign radio value found in sql - Codeigniter

Asked

Viewed 106 times

1

I want the user to be able to see the data you entered in your log when loading the data update page. I can see the data that is in type=text but not in type=radio.

View:

<?php foreach ($resultados_pesquisa as $linha) {
   $nome = $linha["NOME"];
   $genero $linha["GENERO"];
   ?>

<form class="form-horizontal" action="<?php echo site_url('Menu');?>" method="post" accept-charset="utf-8">
          <h3>Dados pessoais</h3>

<!-- Nome -->
  <div class="form-group">
   <label for="inputName" class="col-sm-2 control-label">Nome</label>
    <input type="text" name="nome" value="<?php   echo $nome  ?>" class="form-control" id="inputName" placeholder="Nome"> 
          </div>

<!-- Genero -->
  <div class="form-group">
   <label for="inputGenero" class="col-sm-2 control-label">Genero</label>
     <label id="masculino">
     <input type="radio" name="genero" value="masculino">Masculino
     </label>
     <label id="feminino">
     <input type="radio" name="genero" value="feminino">Feminino
     </label>
   </div>

</form>
 <?php }  ?>

2 answers

0

A solution that you could apply, would be using a Boolean expression inside the radio, to define whether the radio will be checked or not, as follows in the example below, where it can work the variable $genero has to be the Boolean (true or false):

Using ternary operator:

<input type="radio" <?php echo $genero ? 'checked': ''; ?>

Using the if one-line:

<input type="radio" <?php if ($genero) echo 'checked'; ?>

In your code it could look like this:

   <div class="form-group">
   <label for="inputGenero" class="col-sm-2 control-label">Genero</label>
     <label id="masculino">
     <input type="radio" <?php echo $genero ? 'checked': ''; ?> name="genero" value="masculino">Masculino
     </label>
     <label id="feminino">
     <input type="radio" <?php echo $genero ? 'checked': ''; ?> name="genero" value="feminino">Feminino
     </label>
   </div>
  • It didn’t work, but I figured it out. Thank you

0


<input type="radio"
                <?php if (isset($genero) && $genero=="masculino") echo "checked";?>
                 name="genero" value="masculino"> Masculino

Browser other questions tagged

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