Field Completion with ZIP in Cakephp 2.x

Asked

Viewed 646 times

0

I’m trying to auto-fill the fields in Cakephp from a zip code, I’m adapting the code found on this site:

http://matheuspiscioneri.com.br/blog/preenchimento-automatico-do-endereco-a-partir-do-cep/

I used as an example the following site, to know how to configure ajax requests for the Framework:

http://blog.erikfigueiredo.com.br/requisicoes-ajax-com-cakephp-artigo-super-completo/

Here is my code:

Contactoscontroller

class ContatoController extends AppController {
    public $components = array('RequestHandler');

    public $uses = array();

    public function beforeRender()
    {
        if ($this->request->is('ajax')) {
            $this->layout = "ajax";
        }   
    }

    public function consulta_cep() {
        if($this->request->is('post')){
            $reg = simplexml_load_file("http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=" . $cep);

            $dados = $reg->sucesso($this->request->data['resultado']);
            $dados = $reg->rua($this->request->data['tipo_logradouro' . ' ' . 'logradouro']);
            $dados = $reg->bairro($this->request->data['bairro']);
            $dados = $reg->cidade($this->request->data['cidade']);
            $dados = $reg->estado($this->request->data['uf']);

            echo json_encode($dados);
        }
    }

ctp test.

base_url = 
$(document).ready( function() {
    /* Executa a requisição quando o campo CEP perder o foco */
    $('#cep').blur(function(){
       /* Configura a requisição AJAX */
       $.ajax({
            url : 'consulta_cep()', /* URL que será chamada */ 
            type : 'POST', /* Tipo da requisição */ 
            data: 'cep=' + $('#cep').val(), /* dado que será enviado via POST */
            dataType: 'json', /* Tipo de transmissão */
            success: function(data){
                if(data.sucesso == 1){
                    $('#rua').val(data.rua);
                    $('#bairro').val(data.bairro);
                    $('#cidade').val(data.cidade);
                    $('#estado').val(data.estado);

                    $('#numero').focus();
                }
            }
       });   
    return false;    
    })
});

<fieldset>
    <?php
        echo $this->Form->create('Contato');
    ?>
    <? php
        echo $this->Form->input('cep');
        echo $this->Form->input('rua');
        echo $this->Form->input('numero');
        echo $this->Form->input('bairro');
        echo $this->Form->input('cidade');
        echo $this->Form->input('estado');
    ?>
</fieldset>

Routes.php

Router::parseExtensions('json');

I’m having some doubts:

  1. At the url of test view.ctp what I should put, because I want to call the function consulta_cep which is in the Contactocontroller

  2. The way I am doing in the controller to play the data that returns in the $reg variable to the $data variable is correct, because in the site I used as an example it is converting to String before assigning the values to the $data variable?

I’d like your help, because I haven’t found any examples on the Internet for Cakephp to fill in the data from the ZIP code and I’m trying to help the community with the knowledge I have in the Framework, so you can use a basic feature, but that helps and very developers in their projects.

Thank you

  • Particularly I’m not a fan of u8sar the ajax cake requisitions, usually I build my own with jQuery even. Although I use Cake in most of the projects I work with, javascript is one of the things I don’t like to do for cake, even because it’s much more boring to use the Grunt that way. My advice is to make the ajax request and the manipulation on a JS same.

  • I agree, Erlon. But that’s not what he’s doing anymore?

  • @bfavaretto, yes in part, in the case the intention was to say that he did not even need to create a method in the cake to parse the XML. All he has to do is parse the XML no js.

  • @Erloncharles could give me an example of how I can do this?

  • Using the parseXML of jQuery @Rafaelmangueira http://api.jquery.com/jquery.parsexml/

No answers

Browser other questions tagged

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