How to use the select form on the html page

Asked

Viewed 139 times

1

I need to know what the user has chosen within a <select>.

In the code below I need to know the code of the collection chosen by the user, to show the items that have not yet been collected.

<p align="left">Nº da Coleta <br>
<?php 
      echo ("<select name=\"colCodigo\">");

      $coletas = new Coletas();
      $coletas = $coletas->distinct();

      foreach ($coletas as $c) 
      {
          $cod = $c["colCodigo"];

          echo ("<option>$cod</option>");
      }
      echo ("</select>");
?>
  • In this code no part serves to know which one is chosen.

  • You need to put a value in his option

1 answer

5


Assuming your table has one id for each collection, your code should look like this:

<p align="left">Nº da Coleta <br>
<?php 
      echo ("<select name=\"colCodigo\">");

      $coletas = new Coletas();
      $coletas = $coletas->distinct();

      foreach ($coletas as $c) 
      {
          $cod = $c["colCodigo"];

          echo ("<option value='".$c["id"]."'>$cod</option>");
      }
      echo ("</select>");
?>

So when your form is sent you can take the value of select.

Assuming it is sent via POST you can take it like this: $valor=$_POST['colCodigo'];

  • Or you can use "Cod itself": echo ("<option value='$cod'>$cod</option>");. Thus the variable $valor (at the time of reading the $_POST) already have the original value of the attribute colCodigo.

Browser other questions tagged

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