PHP does not take the auto-fill values

Asked

Viewed 431 times

1

Good morning,

I have a little problem and I hope you can help me:

Busca_cep

<!-- Adicionando JQuery -->
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>

<!-- Adicionando Javascript -->
<script type="text/javascript" >

    $(document).ready(function() {

        function limpa_formulário_cep() {
            // Limpa valores do formulário de cep.
            $("#rua").val("");
            $("#bairro").val("");
            $("#cidade").val("");
            $("#uf").val("");
            $("#ibge").val("");
        }

        //Quando o campo cep perde o foco.
        $("#cep").blur(function() {

            //Nova variável com valor do campo "cep".
            var cep = $(this).val();

            //Verifica se campo cep possui valor informado.
            if (cep != "") {

                //Expressão regular para validar o CEP.
                var validacep = /^[0-9]{5}-?[0-9]{3}$/;

                //Valida o formato do CEP.
                if(validacep.test(cep)) {

                    //Preenche os campos com "..." enquanto consulta webservice.
                    $("#rua").val("...")
                    $("#bairro").val("...")
                    $("#cidade").val("...")
                    $("#uf").val("...")
                    $("#ibge").val("...")

                    //Consulta o webservice viacep.com.br/
                    $.getJSON("//viacep.com.br/ws/"+ cep +"/json/?callback=?", function(dados) {

                        if (!("erro" in dados)) {
                            //Atualiza os campos com os valores da consulta.
                            $("#rua").val(dados.logradouro);
                            $("#bairro").val(dados.bairro);
                            $("#cidade").val(dados.localidade);
                            $("#uf").val(dados.uf);
                            $("#ibge").val(dados.ibge);
                        } //end if.
                        else {
                            //CEP pesquisado não foi encontrado.
                            limpa_formulário_cep();
                            alert("CEP não encontrado.");
                        }
                    });
                } //end if.
                else {
                    //cep é inválido.
                    limpa_formulário_cep();
                    alert("Formato de CEP inválido.");
                }
            } //end if.
            else {
                //cep sem valor, limpa formulário.
                limpa_formulário_cep();
            }
        });
    });

</script>
</head>

<body>
<!-- Inicio do formulario -->
  <form method="get" action=".">
    <label>Cep:
    <input name="cep" type="text" id="cep" value="" size="10" maxlength="9" /></label><br />
    <label>Rua:
    <input name="rua" type="text" id="rua" size="60" /></label><br />
    <label>Bairro:
    <input name="bairro" type="text" id="bairro" size="40" /></label><br />
    <label>Cidade:
    <input name="cidade" type="text" id="cidade" size="40" /></label><br />
    <label>Estado:
    <input name="uf" type="text" id="uf" size="2" /></label><br />
    <label>IBGE:
    <input name="ibge" type="text" id="ibge" size="8" /></label><br />
  </form>
</body>

I’m using this code provided by the site http://viacep.com.br/exemplo/jquery/, it is working perfectly, but when I try to give a $_POST['Uf'] or any other input that is not cep in a PHP file, the field appears blank to me.

Would you have any ideas to help me?

Grateful

2 answers

1

Error is due to method defined in tag .

Is like GET

<form method="get" action=".">

Switch to POST

<form method="post" action=".">
  • I had already noticed this fact and switched to post, but at the time of my action, which I call PHP, I can not capture the value. I can only capture the typed values. I thank you for your help.

0

I was able to solve my problem, as the filling was not coming to php I made a request with the Curl inside my php page to the address viacep.com.br/Ws/"cep of the post request here"/Piped/ and I was able to fill in the rest of the fields that were without data.

Browser other questions tagged

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