Popular input type according to Selection made in Combobox (OPTION)

0

When selecting a user in my select, I need to pass the one I selected to the fields in my form. Below is my method that takes this information.

public function LerDadosUsu(){
    try{
        $lerusu = "SELECT * FROM usuario ";
        $listusu = $this->con->Connect()->prepare($lerusu);
        $listusu->execute();
        $retDados = $listusu->fetchAll(PDO::FETCH_ASSOC);
        return $retDados;
        }
     catch(PDOException $erro_2){
     echo 'erro'.$erro_2->getMessage();
        }

    }

Below I want to show all the user field data I selected in my select form

<div class="modal-body">
              <form  action="" method="post" name="frm_usuario">

              <div class="form-group">
             <label for="filter">Filtrar Usuário</label>
             <div class="input-group"><span class="input-group-addon"><i class="fa fa-search"></i></span>

             <select class="form-control" id="sel_usuario">
             <!--INICIO DA PESQUISA DE DADOS COMBOBOX-->
             <?php
             $objeto = new Usuario();
             $usuario = $objeto->LerDadosUsu();
             foreach($usuario as $usuario):
             ?>
  <option value=""> <?php print $usuario['nom_usujauport'] ?></option>

             <?php
             endforeach;
             ?>


             </select>
             <!--PESQUISA DADOS E RETORNA COMBOBOX-->
             </div>

My question is how to pass the data I selected in the combobox to the input type="text" of my fields below Name: Email: Password: Image:

    
asked by anonymous 11.06.2017 / 02:50

1 answer

0

You can solve this with an ajax. In your select users you will use the "change" event to trigger ajax. In the value of the options you must put the user ID to use the same in ajax.

// AJAX
jQuery(document).ready(function($){
    $('#sel_usuario').change(function(){
        var id_user = $(this).val();
        $.ajax({
            url: 'arquivo.php',
            method: 'POST', // Default: GET
            data: { id: id_user }, // ID do usuário que você vai 
            success: function(response) {
                // Em caso de sucesso você vai pegar os dados retornados pelo Ajax e popular seus inputs.
                $('#nome').val(response.nome);
                $('#email').val(response.email);
            },
            error: function() {
                // Tratar os erros
            }
        });
    });
});

PHP file.

function getUser( $id ) {

    if ( is_null($id) ) {
        return false;
    }

    $sql = "SELECT * FROM usuario WHERE id =" . $id;
    $con = $this->con->Connect()->prepare($sql);
    $con->execute();

    $usuario = $con->fetch(PDO::FETCH_OBJ);

    return json_encode( $usuario ); 
}
    
12.06.2017 / 13:52