Use select data in input

Asked

Viewed 188 times

2

I have a code that selects an option of the data coming from the database. This part of the code is ok, it selects and brings.

The problem is that by selecting this option, need to fill 4 fields input automatically, with data coming from the database.

I can only show a data. I believe I have to store this data to use in inputs, but I don’t know how to.

I will post the code below commenting:

<?php


    require_once('requerimentoController.class.php');
        $requerimento = new requerimentoController;
        $acervoGrid = $requerimento->gridAcervos();//Aqui recebo os dados do BD
        $qtdRow = count($acervoGrid);
?>
<html>
    <script>
    function selecionar(){
        document.getElementById('tipo').value = document.getElementById('selectOK').value;
    }
    </script>
    <body>
    <select id="selectOK" onchange="selecionar()">
        <?php for($a = '0'; $a < $qtdRow; $a++){ ?> 

    <option value="<?=$acervoGrid[$a]['tipo'];?>"><?php echo $acervoGrid[$a]['tipo']; ?>
    /* Aqui seleciono qual o valor que quero da tabela*/
    <?php }?>
    </select></br>
    <input type="text" id="tipo"/> /*aqui mostro no input os valores correspondentes ao que selecionei no option com dados que vem do banco.
    <input type="text" id="marca"/>
    <input type="text" id="serie"/>
    <input type="text" id="modelo"/>
    </body>
</html>

1 answer

0


Friend, you will need to use a javascript. When selecting the act in inputs. I have an example for you:

<script>
$('#selectOK').change(function (){
var e = document.getElementById('selectOK');
var strDocumento = e.options[e.selectedIndex].value;

    if(strDocumento == "resultado1"){
        document.getElementById("tipo").value = "VALOR TIPO";
        document.getElementById("marca").value = "VALOR MARCA";
        /* etc..*/
    }
    if(strDocumento == "resultado2"){
        document.getElementById("tipo").value = "VALOR TIPO";
        document.getElementById("marca").value = "VALOR MARCA";
        /* etc..*/
    }
  });
</script>
  • Gabriel, in case I need to go through an array to check which fields and show them. For example, I have in the database the column A B C and in each column of these I have data type, serial tag. If I select option B it should show in the input type, tag and series. If it is option A, same thing.

Browser other questions tagged

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