how it returns pata html array via ajax/php

Asked

Viewed 109 times

1

i have the function below in functions.php files

function get_ADRESS(){
    if(isset($_POST['getcep']) and $_SERVER['REQUEST_METHOD'] == "POST"){   
        $cep = escape($_POST['cep']); //10   
        $cep = preg_replace("/[^0-9]/", "", $cep);
        $url = "http://viacep.com.br/ws/$cep/xml/";
        $xml = @simplexml_load_file($url);
        echo $xml;
    }
}

}

$XML is a matrix that returns me zip, street, Uf, number. What I need to do is run the get_addressee function on the Register.php page and then enter these values in the HTML below, how can I do this ?

<div class="form-group">
    <div class="col-lg-10">           
    <input type="text" class="form-control" placeholder="Endereço" id="cep_load" name="cep_load" value="{$cep}">
    </div>
 </div>  


<div class="form-group">
    <div class="col-lg-10">           
    <input type="text" class="form-control" placeholder="Endereço" id="address" name="address" value="{$rua}">
    </div>
 </div>  
 <div class="form-group">

    <div class="col-lg-10">

     <input class="form-control" id="city" name="city"  placeholder="Cidade" value="{$cidade}" >
     </div>
 </div>

1 answer

0

We will have 1 file called getAdressAjax.php

<?php
require 'function.php';
get_ADRESS();
?>

*(I’ll assume your project uses Jquery) **(following example) In this case the request is only called when the user takes Focus from the 'cep_load' field'.

$('#cep_load').blur(function(){
    $.ajax({url: "getAdressAjax.php", success: function(result){
        $('#address').val(result.endereco);
        $('#city').val(result.cidade);
    });
});

If you use pure Javascript just follow examples

Searching "ajax example" on google you already find a lot GOOD.

Browser other questions tagged

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