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

Asked

Viewed 455 times

0

When selecting a user in my select I need to pass what I have selected to the fields of my form.. below this my method that picks up 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 form select

<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:

inserir a descrição da imagem aqui

  • Your question is a little difficult to understand, try to write only what you need so people can help you.

  • I made the change, but the doubt is how to pass the data of what I selected in my select to the fields, name: emai: password: picture.

1 answer

0


You can solve this with an ajax. In your user select you will use the "change" event to trigger the ajax. In the value of the options you must put the user ID to use it 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 ); 
}
  • I did as Kaio Bruno mentioned above...but it did not work, could you mention how I do it in my HTML markups? example as I put user ID in my html option?

  • No man, you did it different and wrong, you can’t put a PHP function on onChange. Look at the example I made and compare with what you did, it’s totally different.

  • If you can please show how I do this in html...only for same comparison method... Thank you.

  • All you will do is in PHP and javascript, as I put in the example.

  • okay, thank you!!

Browser other questions tagged

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