Change an option of a select according to the choice of another select (HTML form)

Asked

Viewed 520 times

0

Hey, here’s the thing.

I have an area of a form in which I select the code of the company, company and also show the taxation of that company.

I will show the image of the fields: (my problem is in the part marked in red)

inserir a descrição da imagem aqui

COD and Enterprise are listed dynamically via PHP and AJAX with a query in the database that lists companies that are the responsibility of the logged-in user. And the Taxation is shown according to the Taxation of the company (Each company can have 5 types of Taxation).

I would like the following, when I changed the COD, the Company corresponding to that Code and its Taxation were changed in select automatically.

And I would also like to see the same thing done when I changed the Company. COD and Taxation were also changed.

Remember that all these data are linked to the same Mysql table. And they are all linked to each other.

I managed to make a "Gambi" in which I almost resolve that situation. When I change the COD, the company and the corresponding Taxation are also changed. However, it is not the same when I change the company. The only thing that changes is taxation.

Note: COD and Companies are <select> and Taxation is a <input type="text">.

I’ll leave my code here (with the "Gambi") to see if you can help me.

//FUNCAO PARA PRENCHER SELECT EMPRESA E TRIBUTAÇÃO DE ACORDO COM O CÓDIGO (COD) ESCOLHIDO

$(function() {
  $("select#codigo-empresa").change(function(e) {
    var cod = $("select#codigo-empresa option:selected").attr('id');
    $("select#nome-empresa").val(cod);

    var trib = $("select#codigo-empresa option:selected").attr('name');
    $("input[id='tributacao']").val(trib);

  });
});


//FUNCAO PARA PRENCHER SELECT COD E TRIBUTAÇÃO DE ACORDO COM A EMPRESA ESCOLHIDA

$(function() {
  $("select#nome-empresa").change(function(e) {
    var codigo = $("select#nome-empresa option:selected").attr('id');
    
    $("select#codigo-empresa").val(codigo);
    
    var tribu = $("select#nome-empresa option:selected").attr('name');
    $("input[id='tributacao']").val(tribu);

  });
});
<!-- LINHA -->
<div class="col-lg-12 alinhar-texto-no-centro" id="selecionar-empresas">
  <h3 class="alinhar-texto-no-centro">Selecionar Empresa</h3>

  <!-- CAMPO CODIGO DA EMPRESA -->
  <div class="form-group col-lg-2">
    <label for="codigo-empresa">COD</label>
    <select name="codigo-empresa" id="codigo-empresa" class="input form-control">
      <?php
        while($registros_cod = mysqli_fetch_assoc($query_pegar_cod)){
      ?>
        <option id="<?php echo $registros_cod['EMPRESAS'];?>" name="<?php echo $registros_cod[" TRIBUTACAO "];?>">
          <?php echo  $registros_cod['COD']; ?>
        </option>
        <?php
        }
				?>
    </select>
  </div>
  <!-- CAMPO EMPRESA -->
  <div class="form-group col-lg-7">
    <label for="nome-empresa">Empresa</label>
    <select name="nome-empresa" id="nome-empresa" class="input form-control">
      <?php
      while($registros_empresas =  mysqli_fetch_assoc($query_pegar_empresa))
        {
      ?>
        <option id="<?php echo utf8_encode($registros_empresas['EMPRESAS']);?> " name="<?php echo utf8_encode($registros_empresas[" TRIBUTACAO "]); ?> ">
      <?php echo utf8_encode($registros_empresas['EMPRESAS']); ?>
        </option>
        <?php
        }
        ?>
    </select>
  </div>
  <!-- CAMPO TRIBUTAÇÃO -->
  <div class="form-group col-lg-3">
    <label for="tributacao">Tributação</label>
    <input name="tributacao" id="tributacao" class="input form-control" readonly></input>
  </div>
  <!-- CAMPO POSSUIR ARQUIVOS -->
  <div class="form-group col-lg-3 col-lg-offset-9" id="lista-empresas" style="display:inline">
    <label for="nome-empresa">Listar Empresas</label>
    <select name="empresas-filtro" id="empresas-filtro" class="input form-control">
      <option>Minhas</option>
      <option>Todas</option>
    </select>
  </div>
</div>
<!-- LINHA -->

Thanks in advance. Thank you!

  • How you are doing to search the data in the bank when changing the option?

  • I’m not picking up the data in the database. I assign values to the ID and NAME of the options. So when I change the option of a select, then I add the ID of that field to the option of the other select. Remembering that the values and ID of the two selects are the result of a query in the database. For example, as the id of the COD opstion I put the name of the company corresponding to that code. And as the id of the Companies option, I put the COD corresponding to the name of this company. I don’t know if I could be clear ...

  • If there’s an easier way to do this action, I accept.

  • Probably the answer is very simple, but I found it difficult to understand what you want to do. The data that you want to be displayed or not, is not coming from the correct database? would create arrays with options not solve your problem? That is, when choosing an option the other select would be populated with array A if choosing another option the other select would be populated with array B.

  • Vinícius, each select of that has about 50 items. One select listing companies and the other select listing the company code. I’ll try to be as clear as I can right now. I would like when I selected the company "A" in the first select, the code "1" was automatically selected in the second select. And when I selected the code "10" in the second select, the company "J" was automatically selected in the first select. And so on.(Remembering that A is the first letter of the alphabet and the letter J is the 10th). I tried to be as clear as possible now. The data in the options is coming from the bank.

No answers

Browser other questions tagged

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