To assign the chosen value in a select (HTML) to a php variable

Asked

Viewed 2,402 times

1

I have a code that I need to take the value selected by the user and my code always takes the last select code.

<?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;
}
?>

3 answers

3

Taking into account @hugomg’s response, and considering that select is already inside the form, there is one more fix to do. Your select is mounted in the wrong way, the name should stay in the select and not in the option, example:

<?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;
}
?>
  • I don’t understand, @abfurlan. Your code is almost the same as mine, except for the name in select. I made this change to keep the pattern and it hasn’t changed anything. The big problem is: '$tipo_info = $reg["cod_tipo_info"];' If you are going to do the table test or the Chinese test, it will go through while and will include one, two, three and get the result 4. Maybe I have to create an array list. Someone can help me?!?

  • @fabricio_wm, then your problem is another and not picking the selected value in select, it is not very clear the problem, perhaps it would be interesting to edit the question and try to be more specific

  • @fabricio_wm If I understand, you want it to be released to submit the form only if the user selects the option with select value 4, that’s it?

  • Yes. Pq the other buttons are type="button", that is, they do nothing. Only option 4 that will give an Submit pq the type="Submit".

  • I created a form with PK in the head of the form and this way, the system administrator will be able to insert questions in the amount she wants and will select for question 1 the answer can be of type select, Yes or no (radio button) or a note. Got it?

  • Look at this: <br/> I don’t want to end the existing programming here. http://jsfiddle.net/U7Vvb/4/ But I did get another way. http://jsfiddle.net/dieegov/kN84b/2/

  • @fabricio_wm, yes I would suggest to do as javascript, more I advise you to treat the value of the post in php.

Show 2 more comments

1

On the HTML page you co-install, place the select within an HTML form.

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

When the user subends the form, the browser will make an HTTP request at the address specified by the attribute action, 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 run the PHP script of the page the form requested. The form data will be in the variable $_POST, which is a hash that takes the name of the form element (which you specified using the name attribute) and returns the value that the user placed in that field.

  • Yes. It is inside a form. I thought I didn’t need to copy all the code. The code, you must submit only if the person chooses option 4 - Finish. Thank you.

  • Son, I’m not here to do your homework. It is impossible to prevent the user from sending incorrect data (never trust the data you receive! ) then what you will have to do is see if the user sent option 4 and if he has not sent the form again with a message saying that he has invalid his data. (You can even use Javascript to make the browser prevent submission but it is only useful for performance, since the client still has ways to send you an invalid form if he really wants to)

  • I don’t want anyone to do my homework. I don’t think you understand the logic. There is no wrong option. According to the selected option, it will have a behavior. I think a forum, is to learn from each other in a humble way. Maybe this is too simple for you. If you remove the PHP code, you will see through the code http://jsfiddle.net/U7Vvb/4/ that not every button, does Submit. But this is common when we don’t analyze the code or replace people. I may not be good at php, but I know how to program.

  • 1

    Sorry, I thought the ending was a command for me, not the computer. : -/ Sorry again, I was really rude.

  • 1

    I’m sorry too, @hugomg. Tb was rude. I don’t want stress or trouble. Stay with God.

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;

See this Post here

Browser other questions tagged

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