Database search with value of <select>

Asked

Viewed 513 times

3

How to perform a search in the database, through a parameter informed by the tag select?

Follow the code below:

   <select  class="form-control" id="inputUnidade">
     <option>Selecione</option>
     <option>*GCOI</option>
     <option>ESC - RJ</option>
  </select>

Example, if the user chooses "ESC-RJ", search only the records that are "ESC-RJ". Any hint?

  • Welcome to the Sopt. Add to the question by clicking on [Edit] your attempt to build the query as well as the structure(s) tables from which you want to extract the information.

2 answers

5

HTML

 <select  class="form-control" id="inputUnidade" name="inputUnidade">
       <option>Selecione</option>
       <option>*GCOI</option>
       <option>ESC - RJ</option>
 </select>

PHP

 <?php
      $unidade = $_POST['inputUnidade'];

      $sql = "SELECT * FROM Tabela WHERE Unidade = '$unidade'";

0

The problem with your code is that your options, are incomplete :

<select  class="form-control" id="inputUnidade" name="inputUnidade">
    <option value="{valor que sera atribuido ao select}">{alias}</option>
</select>

She must have the attribute value so that the select has the value selected.

Thus :

<select  class="form-control" id="inputUnidade">
    <option value="">Selecione</option>
    <option value="*GCOI">*GCOI</option>
    <option value="ESC - RJ">ESC - RJ</option>
</select>
  • 1

    When the option description has the same value as the value, it can be omitted without problem. https://www.w3.org/wiki/HTML/Elements/option#Html_attributes

  • @rray was unaware of this fact, thanks for the information :D

  • Your first code solved the problem but commented on it, by default no value(tag input/select) is sent if you do not have the name, in the question code only id;)

Browser other questions tagged

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