show input when selecting an optin within the while of php

0

Good morning!

Personal is as follows, I have a combobox that displays all the records of a table, until that part I could unroll. However, I need to show an input when one of these records is selected and hide this same input when nothing is selected. My difficulty is when it takes the option value and make the comparison, it follows the code of combo .

<label for="empresa">empresas *</label></a><br/>  
     <select name="empresa" id="empresa" 
      onchange="ExibirDiv(this.value)" class="form-control">
       <option value="">SELECIONE</option>
          <?php
             $parametro_empresa = filter_input(INPUT_GET,"parametro_empresa");
             $empresa = "SELECT * FROM tb_empresa WHERE 
             razaosocial_pessoafisica LIKE '%" . $parametro_empresa . "%'";                                          
             $recebe_empresas = mysqli_query($con, $empresa);
              while ($linha = mysqli_fetch_array($recebe_empresas)) {
               echo '<option value="' . $linha['codigo_empresa'] . '">' . 
               $linha['razaosocial_pessoafisica'] . '</option>';
             }
          ?>
     </select>
    
asked by anonymous 13.08.2018 / 17:05

2 answers

1

Check if there is a return of the value of any selected% with% of whose%% is different from null.

function valida() {
var x = document.getElementById("empresa");
  if (x.options[x.selectedIndex].value != "" ){
      document.getElementById('idInput').style.display = "block";
  } else { 
      document.getElementById('idInput').style.display = "none";
  } 
}
<label for="empresa">empresas *</label></a><br/>  
     <select name="empresa" id="empresa" 
      onchange="valida()" class="form-control">
       <option value="">SELECIONE</option>
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
     </select>

  <input type="text" id="idInput"  style="display: none;">

In fact, in

 if (x.options[x.selectedIndex].value != "" ){

You can put any value in option such as value

In this case the option whose value is null

<option value="">SELECIONE</option> 

should contain the value .value != "" , ie

<option value="qqvalor">SELECIONE</option>

Example:

function valida() {
var x = document.getElementById("empresa");
  if (x.options[x.selectedIndex].value != "qqvalor" ){
      document.getElementById('idInput').style.display = "block";
  } else { 
      document.getElementById('idInput').style.display = "none";
  } 
}
<label for="empresa">empresas *</label></a><br/>  
     <select name="empresa" id="empresa" 
      onchange="valida()" class="form-control">
       <option value="qqvalor">SELECIONE</option>
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
     </select>

  <input type="text" id="idInput"  style="display: none;">
    
13.08.2018 / 17:58
0

Hello @ user122281, you can do as follows:

<?php
            $parametro_empresa = filter_input(INPUT_GET,"parametro_empresa");
             $empresa = "SELECT * FROM tb_empresa WHERE 
             razaosocial_pessoafisica LIKE '%" . $parametro_empresa . "%'";                                          

           $recebe_empresas = mysqli_query($con, $empresa);
              while ($linha = mysqli_fetch_array($recebe_empresas)) {

               $options [] = '<option value="' . $linha['codigo_empresa'] . '">' . 
               $linha['razaosocial_pessoafisica'] . '</option>';

               if($linha['codigo_empresa']==""){
                   $selected_option []="<option value=''>Selecione</option>"; 
               }else{
                   $selected_option []="<option value='$linha['codigo_empresa']'>$linha['razaosocial_pessoafisica']</option>"; 
               }
             }
          ?>

<label for="empresa">empresas *</label></a><br/>  
     <select name="empresa" id="empresa" 
      onchange="ExibirDiv(this.value)" class="form-control">
          <?php echo join($selected_option); ?>
          <?php echo join($options);  ?>
     </select>
  • For the $ selected_option, place the related where ID in the sql to filter the specific value, or create a sql for that purpose.
13.08.2018 / 18:25