Question on form involving select

0

Hello. I have a question where I need to get information from the user and send it to php. I am not getting the information of the user's choice. He must choose whether he is a client or an employee in the options. I would like to know if it is possible to send this information, because I have a table called a post in the bank where I keep the user or a client.

Form

<form class="" method="post" action="controleUsuario.php">
        <h1 class=""><b>Cadastrar</b></h1>
        <div class="form-group"> <label for="nome">Nome:</label> <input type="text" class="form-control" name="nome" id="nome" placeholder="Digite seu nome"> </div>
        <div class="form-group"> <label for="email">Email:</label> <input type="text" class="form-control" id="email" name="email" placeholder="Digite seu Email"> </div>
        <div class="form-group"> <label for="senha">Senha:</label> <input type="password" class="form-control" id="senha" name="senha" placeholder="Digite sua senha"> </div>
        <label class="mr-sm-2 sr-only" for="escolha">Escolha:</label>
        <div class="form-row align-items-left">
        <div class="col-auto my-1">
  <select class="custom-select mr-sm-2" name="escolha" id="escolha">
    <option name="cliente" id="cliente" value="Cliente">Cliente</option>
    <option name="funcionario" id="funcionario" value="Funcionario">Funcionario</option>
  </select>
  </div>
</div>

        <button type="submit" name="opcao" value="Cadastrar" class="btn btn-primary">Comfirmar</button>
      </form>

User Control

if(isset($_POST["opcao"])){

$opcao = $_POST["opcao"];
$escolha = $_POST["escolha"];

if($opcao == "Cadastrar"){
    if($escolha=="Funcionario"){
    $funcionario=$_POST["funcionario"]; 
    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $senha =sha1($_POST["senha"]);
    cadastroFuncionario($nome,$email,$senha,$funcionario);
    header("Location: loginUsuario.php");
    }else if($escolha=="Cliente"){
    $cliente=$_POST["cliente"]; 
    $nome = $_POST["nome"];
    $email = $_POST["email"];
    $senha =sha1($_POST["senha"]);
    cadastroCliente($nome,$email,$senha,$cliente);
    header("Location: loginUsuario.php");   
    }

}
    
asked by anonymous 10.11.2018 / 19:19

1 answer

0

The options are the different options that a select has, expressed with the tag <OPTION>

<select name="NOME" size="ALTURA">
<option value="VALOR A PASSAR">VALOR MOSTRADO</option>
<option value="VALOR A PASSAR">VALOR MOSTRADO</option>
<option value="VALOR A PASSAR" SELECTED>VALOR MOSTRADO</option>
</select>

Option tag attributes:

  • (value): With this attribute, we define the information that each item will send to the server, if it is selected.
  • (selected): With this attribute, we define between the inserted items which will appear previously selected.
  

Do not use name in options

    <select class="custom-select mr-sm-2" name="escolha" id="escolha">
      <option name="cliente" id="cliente" value="Cliente">Cliente</option>
      <option name="funcionario" id="funcionario" value="Funcionario">Funcionario</option>
    </select>
  

so something like $funcionario=$_POST["funcionario"]; is useless

When sending the form to PHP, the script responsible for receiving the information will know the name of the form "escolha" and will know the value that was chosen ( Cliente or Funcionário ).

  

PHP will retrieve the value of option checked by name of select

In your case $escolha = $_POST["escolha"];

    
10.11.2018 / 22:45