how to return array pax html via ajax / php

1

I have the function below in the 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 an array that returns me zip, street, uf, number. What do I need to run the get_endereco function on the register.php page and then enter those values in the HTML below, how do I do that?

<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>
    
asked by anonymous 14.02.2017 / 11:38

1 answer

0

Let's have 1 file called getAdressAjax.php

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

* (I'll assume your project uses JQuery) ** (follow example) In this case the request only is called when the user takes the 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

Looking for "ajax example" in google you already think a lot of GOOD.

    
14.02.2017 / 12:35