PHP/ HTML zip code search

Asked

Viewed 561 times

-4

What is missing for this code bring the result of the zip code ?

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <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>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />

    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cp));

        echo "<br><br>CEP Informado: $cep<br>";
        echo "Rua: $addres->logradoro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $adress->uf<br>";
    }

    function get_address($cep) {


        $cep = preg_replace("/[^0-9]/", "", $cep);
        $url = "http://viacep.com.br/ws$cep/xml/";

        $xml = simplexml_load_file($url);
        return $xml;
    }
    ?>
</body>
</html>


I made a new version with corrections and inserted the class Address , but is not yet returning to Street, Neighborhood and State

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <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>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />

    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cep));

        echo "<br><br>CEP Informado: $cep <br>";
        echo "Rua: $addres->logradoro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $address->uf<br>";
    }

    class Address {

        public $bairro;
        public $logradouro;
        public $uf;

        function get_address($cep) {


            $url = "http://viacep.com.br/ws/" . $cep . "/xml";
            $xml = simplexml_load_file($url);
            $this->bairro = $xml->bairro;
            $this->logradouro = $xml->logradouro;
            $this->uf = $xml->uf;
        }

    }
    ?>
</body>
</html>

  • Welcome Raquel Gomes, if any answer solves your problem mark it as accepted. See how in https://i.stack.Imgur.com/jx7Ts.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

  • Take a tour https://answall.com/tour

2 answers

1

Your code has several typos.

it’s not $cp

$address = (get_address($cp));

and yes $cep

$address = (get_address($cep));

it’s not $addres or $Adress

echo "Rua: $addres->logradoro<br>";
echo "Estado: $adress->uf<br>";

yes $address

echo "Rua: $address->logradoro<br>";
echo "Estado: $address->uf<br>";

It is not cheating

echo "Rua: $address->logradoro<br>";

and yes patio

echo "Rua: $address->logradouro<br>";

The address http://viacep.com.br/ws$cep/xml/ is wrong,

the correct is http://viacep.com.br/ws/$cep/xml/

Where is the tag <form>...</form> of your form?

Use this code that will work:

<?php
function get_endereco($cep){
  // formatar o cep removendo caracteres nao numericos
  $cep = preg_replace("/[^0-9]/", "", $cep);
  $url = "http://viacep.com.br/ws/$cep/xml/";
  $xml = simplexml_load_file($url);
  return $xml;
}
?>
<meta charset="UTF-8">
<h1>Pesquisar Endereço</h1>
<form action="" method="post">
  <input type="text" name="cep">
  <button type="submit">Pesquisar Endere&ccedil;o</button>
</form>


<?php if($_POST['cep']){ ?>
<h2>Resultado da Pesquisa</h2>
<p>
  <?php $endereco = get_endereco($_POST['cep']); ?>
  <b>CEP: </b> <?php echo $endereco->cep; ?><br>
  <b>Logradouro: </b> <?php echo $endereco->logradouro; ?><br>
  <b>Bairro: </b> <?php echo $endereco->bairro; ?><br>
  <b>Localidade: </b> <?php echo $endereco->localidade; ?><br>
  <b>UF: </b> <?php echo $endereco->uf; ?><br>
</p>
<?php } ?>

OR IF YOU PREFER TO USE YOUR CORRECTED

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>
        
<form action="" method="post">        
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <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>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />
</form>
    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cep));

        echo "<br><br>CEP Informado: $cep<br>";
        echo "Rua: $address->logradouro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $address->uf<br>";
    }

    function get_address($cep) {


        $cep = preg_replace("/[^0-9]/", "", $cep);
        $url = "http://viacep.com.br/ws/$cep/xml/";

        $xml = simplexml_load_file($url);
        return $xml;
    }
    ?>
</body>
</html>
  • Thank you very much !

-2

  • I performed the modification and also modified the echo section "Status: $address->Uf<br>"; Does it have to do with how php shows the information ?

Browser other questions tagged

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