How to popular a <select> dynamically?

Asked

Viewed 47 times

0

I’m trying to create a <select>, where when selecting the estado, the even displays the cidades in another field. The estados are coming up to mine controller, however is not displayed in the field of <select>.

That is, in the <select name="estado"> the same are not returned, but if I use the var_dump in the options_estados in the controller, the same are returned.

What is my difficulty?

  • a) Find out what problem is occurring.
  • b) Display data in my field within my view.

MY CONTROLLER

public function create() {
    $variaveis['titulo'] = 'Novo Cadastro';
    $this->load->model("m_estados_cidades");
    $estados = $this->m_estados_cidades->retorna_estados();

    $option = "<option value=''></option>";
    foreach($estados -> result() as $linha) {
        $option .= "<option value='$linha->id'>$linha->nome</option>"; 
    }

    $variaveis['options_estados'] = $option;
    $this->load->view('v_cadastro', $variaveis); 
}

MY VIEW

<div class="form-group">
    <label for="estado">Estado</label>
     <select id="estado" name="estado" class="form-control">
         <?php echo $options_estados; ?>
     </select>
</div>
  • have you tried <option value='{$linha->id}'>{$linha->nome}</option> ?

  • I’ve tried and continues the same way, the states don’t come.

No answers

Browser other questions tagged

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