How to assign the selected value in a select (HTML) to a variable in php

1

I have a code that needs to get the value selected by the user and my code always picks the last code from the select.

<?php
$tipo_info = 0;
echo "<fieldset>Selecione, qual o tipo da informação que deseja cadastrar.<br />";
$query = $con->query("SELECT * FROM pv_tipo_info_questionario");
echo "<select>";
echo "<option></option>";
while($reg = $query->fetch_array()) {
  echo "<option name='ativo' value='".$reg["cod_tipo_info"]."'>".$reg["tipo_info_questionario"]."</option>";
  $tipo_info = $reg["cod_tipo_info"];
}
echo "</select>";
echo "<br /><br />";

switch ($tipo_info){
  case 1:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 2:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 3:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 4:
    echo "<button type=\"submit\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
}
?>
    
asked by anonymous 02.07.2014 / 19:23

3 answers

3

Considering @hugomg's response, and considering that select is already within the form, there is one more correction to make. Your select is set incorrectly, name must be in select and not option , eg:

<?php
$tipo_info = 0;
echo "<fieldset>Selecione, qual o tipo da informação que deseja cadastrar.<br />";
$query = $con->query("SELECT * FROM pv_tipo_info_questionario");
echo "<select name='ativo'>"; //AQUI O NOME DO SELECT
echo "<option></option>";
while($reg = $query->fetch_array()) {
  echo "<option value='".$reg["cod_tipo_info"]."'>".$reg["tipo_info_questionario"]."</option>";
  $tipo_info = $reg["cod_tipo_info"];
}
echo "</select>";
echo "<br /><br />";

switch ($tipo_info){
  case 1:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 2:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 3:
    echo "<button type=\"button\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
  case 4:
    echo "<button type=\"submit\" disabled=\"true\" class=\"avancar btn btn-primary pull-right\">Avancar</button>";
    break;
}
?>
    
02.07.2014 / 19:37
1

In the HTML page that you create, place the select inside an HTML form.

<form name="meuFormulario" action="umapagina.php" method="POST">

When the user submits the form, the browser will make an HTTP request at the address specified by the action attribute, using the POST method. Up to this point it works the same way for any web programming language. After that, your server will receive this request and will execute the PHP script of the page that the requested form. The form data will be in the $_POST variable, which is a hash that receives the name of the form element (which you specified using the name attribute) and returns the value that the user put in that field.

    
02.07.2014 / 19:31
0

A possible solution would be to create a function that always selects the chosen value whenever the form is submitted see this example

function selected($value, $selected) {
    return $value == $selected ? ' selected="selected"' : '';
}

You can use it that way

<pre>
<select class="span6 m-wrap" name="genero">
          <option value="">-Seleciona um genero-</option>
          <option value="Masculino"
        <?php
       if (isset($valor) && !empty($valor)):
 echo selected($valor['genero'], "Masculino"); endif;?> >Masculino
</option>
        <option value="Femenino"
 <?php
     if (isset($valor) && !empty($valor)):
 echo selected($valor['genero'], "Femenino"); endif;?> >Femenino
</option>
  </select>  
</pre>
a variavael valor é onde vai estar guardado armazenado o teu Post ou seja
$valor=$_Post;

View This Post here

    
03.08.2015 / 00:43