Retrieve selected value from a select in the database

0

I can not retrieve the value that is written from select .

I have this recovery by input that is working normally.

<td>
   <div class="drop-down-select opcoes ">
      <label for="bairro">Bairro:</label>
      <input type="text" size="50"  class="form-control" id="bairro" name="bairro" value = "<?php echo $bairro;?>" >
   </div>
</td>
<td>
   <div class="drop-down-select opcoes ">
      <label for="rua">Rua:</label>								
      <input type="text" size="70"  class="form-control" id="rua" name="rua" value = "<?php echo $rua;?>" >
   </div>									
</td>

But if possible, I wanted to retrieve this data from Select, follow the code below ...

<td width="160">
   <div class="drop-down-select opcoes ">
      <label for="bairro">Bairro:</label>
      <select name="bairro" id="bairro" required>
         <option value="">Selecione</option>
         <?php foreach ($bairros as $bairro) { ?>
         <option value="<?php echo $bairro['id'] ?>"><?php echo $bairro['nome'] ?></option>
         <?php } ?>
      </select>
   </div>
</td>
<td>
   <div class="drop-down-select opcoes ">
      <label for="rua">Rua:</label>
      <select name="rua" id="rua" disabled required>
         <option value="<?php $rua; ?>">Selecione uma Rua</option>
      </select>	
   </div>									
</td>

This is the same dynamic select I use for the cadaster. Thanks in advance.

    
asked by anonymous 04.12.2017 / 14:51

1 answer

0

Is there an error on the console / page? The neighborhood select seems to me correct.

One thing I did not understand in your code is if the $ street is an ARRAY of streets, or if it's just an option. If there is more than one street to choose from, you should use a foreach exactly as you used it in the neighborhood.

I set a part of the HTML code, and gave two options to get the element with JavaScript.

Here you find an example with angle 1 if you are using.

// JS PURO:
var meuElementoHTML = document.getElementById("rua"); // armazena o ELEMENTO HTML com id RUA na variável meuElementoHTML
var value = meuElementoHTML.options[meuElementoHTML.selectedIndex].value;
console.log(meuElementoHTM);
console.log(value);

// J QUERY:
$("#rua :selected").text(); // Texto exibido nesse elemento
$("#rua").val(); // Valor da opção selecionada
<td width="160">
   <div class="drop-down-select opcoes ">
      <label for="bairro">Bairro:</label>
      <select name="bairro" id="bairro" required>
         <option value="">Selecione</option>
         <?php foreach ($bairros as $bairro) { ?>
         <option value="<?php echo $bairro['id'] ?>"><?php echo $bairro['nome'] ?></option>
         <?php } ?>
      </select>
   </div>
</td>
<td>
   <div class="drop-down-select opcoes ">
      <label for="rua">Rua:</label>
      <select name="rua" id="rua" required>
      <option value="">Selecione uma Rua</option>
         <option value="<?php echo $rua; ?>"><?php echo $rua?></option> <!-- TINHA FALTADO O ECHO PRA PRINTAR A VARIAVEL RUA -->
      </select>	
   </div>									
</td>
    
04.12.2017 / 17:27