Cakephp - Recover option value

Asked

Viewed 382 times

2

Hello! I’m fixing a website that I picked up ready and in it has a part that the user selects a neighborhood in a <option>, however, this <option> is not sending the information to the database.

Selecting the city, the neighborhoods of this particular city appear. Here is the file view:

echo $this->Form->input('cidade', array('label' => 'Cidade', 'empty' => 'Selecione uma cidade', 'options' => $Cidades));
echo $this->Form->input('bairro', array('label' => 'Bairro', 'empty' => 'Selecione o Bairro', 'options' => array()));

In the controller, the add function:

public function add () {
    if (empty($this->request->data)); else {
        $data = $this->request->data;
        if ($data['Imovel']['opcoes']) $data['Imovel']['opcoes'] = implode(';', $data['Imovel']['opcoes']);
        $data['Imovel']['ativo'] = $data['Imovel']['ativoPeloAdm'] = 1;
        $data['Imovel']['creator'] = $data['Imovel']['modifier'] = $this->Session->read('Auth.User.Imobiliaria.id');
        if ($data['Imovel']['bairro']) {
            $bairro = $this->Cep->find('first', array('conditions' => array('id' => $data['Imovel']['bairro'])));
            $data['Imovel']['bairroNome'] = $bairro['Cep']['bairro'];
        }

        if ($data['Imovel']['cidade']) {
            $cidade = $this->Cidade->find('first', array('conditions' => array('id' => $data['Imovel']['cidade'])));
            $data['Imovel']['cidadeNome'] = $cidade['Cidade']['nome'];
        }

        $data['Imovel']['especificacao'] = $data['Imovel']['imovel'];
        if ($this->Imovel->save($data)) $this->redirect(array('action' => 'index'));
    }
}

Can someone help me?

Ajax :

    $('#ImovelCidade').change(function(e) {
    $('#ImovelBairro').html($('<option />').val('').text('Carregando...'));
    $.getJSON(
        "<?php echo Router::url(array('controller' => 'pages', 'action' => 'pegarBairros')) ?>", 
        { "cidade" : $(this).val() }, 
        function (data) { 
            $('#ImovelBairro').html($('<option />').val('').text('Selecione'));
            $.each(data, function (chave, valor) {
                $('#ImovelBairro').append($('<option />').val(chave).text(valor));
            } );
        }
    );
}); 

Function pegarbairros in the controller:

public function pegarBairros ($cidade = null) {
    $this->layout = 'json';
    $result = array();

    if (in_array($_REQUEST['cidade'], array_keys($this->cidade))) {
        $bairros = $this->Cep->find('list', array('fields' => array('id','bairro'), 'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']]),'group' => 'bairro'));
        sort($bairros);         

        foreach ($bairros as $id => $bairro) {
            if (!empty($bairro)) 
                $result[$id] = $bairro;
            else 
                $result[] = 'error';
            $this->set('data', $result);
        }
    }
}

I’m trying to enter a neighborhood but is returning an Array

  • It seems to be missing something, usually in this case the consultation by the neighborhoods of the city would be done using ajax. Are you sure it’s all there? There’s no [tag:javascript] missing?

  • You turned on debug level 2 on core.php?

  • @Erloncharles , I will post the js !

1 answer

1

Reading your code seems to me that it is an error in formatting the data. You need your return a so you will have more facility to manipulate the return by and generate a valid.

What I suggest:

public function pegarBairros ($cidade = null) {
    $this->layout = 'json';
    $result = array();

    if (in_array($_REQUEST['cidade'], array_keys($this->cidade))) {
        $bairros = $this->Cep->find('list', array('fields' => array('id','bairro'), 'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']]),'group' => 'bairro'));
        sort($bairros);         

        foreach ($bairros as $id => $bairro) {
            if (!empty($bairro)) {
                $result[$id] = $bairro;
            } else {
                $result[] = 'error';
            }
            $result = json_encode($result);
            $this->set('data', $result);
        }
    }
}
  • I switched to this code and nothing appears, just loading....

  • show me an example of json being returned

  • json is returning the position of the item in the list...

Browser other questions tagged

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