show input when selecting an optin inside while in php

Asked

Viewed 83 times

0

Good morning!

Here’s the thing, I got a combobox which displays all the records of a table, until that part I managed to 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 time to take the option value and make the comparison, 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>
  • Explain yourself better. Show an input when ONE of these options is selected OR when any of these options is selected?

  • opa, actually it is when any of these options is selected

  • Welcome, if any answer has served you be sure to mark it as accepted. See how in https://i.stack.Imgur.com/evLUR.png

  • Read this post to understand how things work around here https://answall.com/help/mcve

2 answers

0

Check if there is return of value of some option selected whose value 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 on .value != "" as an example .value != "qqvalor"

In this case the option whose value is null

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

should contain the value qqvalor, that is to say

<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;">

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 $selected_option, put the related Where ID in sql so that you can find the specific value, or create an sql directed to that purpose.

Browser other questions tagged

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