How to make a dropdown that imports the database options

0

I have a student enrollment form and one for enrollment of these students in classes.

To enroll the student in the class, I would like to have a dropdown where I could select one of the registered students, I thought it could be something simple using select, but searching in forums, I did not understand how to do that.

The form to register the students (collaborators) in the classes:

<div class="container">
    <div class="row">
        <div class="col-lg-12 text-center">
            <h1 style="
                margin-top:100px;">Inscrição</h1>
            <p> </p>
            <p class="lead"></p>
            <ul class="list-unstyled">
                <form id="cadastro" method="post" action="banco/updateP.php" style="
                    text-align: left;
                    margin-top:50px;">
                    <div class="col-lg-12">
                        <div class="form-group" style="
                    text-align: left;">
                            <label  for="FORMACAO">Formação: </label>
                            <input  type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?php echo $nome['NOME']?>">
                         </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="TURMA">Turma: </label>
                            <input  type="text" required class="form-control" id="TURMA" name="TURMA">
                         </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="COLABORADOR">Colaborador: </label>
                            <select  class="form-control" id="COLABORADOR" name="COLABORADOR">
                                <?php
                                    require('class/conexao.php');
                                    $sql = $pdo->prepare("SELECT * FROM colaboradores");
                                    $sql->execute();
                                    while($ln = $sql->fetchObject()){
                                        echo '<option value="'.$ln->ID.'">'.$ln->NOME.'</option>';
                                    }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-lg-12">
                        <div class="form-group" method="post" style="
                    text-align: left;">
                            <label  for="PREVISTO">Status: </label>
                            <input  type="text" required class="form-control" id="PREVISTO" name="PREVISTO" value="Previsto">
                         </div>
                        <div class="form-check form-check-inline disabled">
                            <label class="form-check-label">
                                <input class="form-check-input" type="radio" name="STATUS" id="STATUS" value="Realizado" disabled> Realizado
                            </label>
                        </div>
                        <div class="">
                            <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
                        </div>
                    </div>
                </form>
            </ul>
        </div>
    </div>
</div> 

The bank where the students (collaborators) are registered:

<?php

    $nome           = $_POST['NOME'];
    $identifiant    = $_POST['IDENTIFIANT'];
    $turma          = $_POST['TURMA'];
    $status         = $_POST['STATUS'];

    $strcon = mysqli_connect('localhost','root','', 'db_formacao') or die('Erro ao conectar ao banco de dados');
    $sql = "INSERT INTO colaboradores VALUES ('$id','$nome', '$identifiant', '$turma', '$status')"; 
    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    mysqli_close($strcon);

    echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';

?>

The bank that will save the form that will enroll the student in the class:

<?php

    $formacao       = $_POST['FORMACAO'];
    $turma          = $_POST['TURMA'];
    $colaborador    = $_POST['COLABORADOR'];
    $previsto       = $_POST['PREVISTO'];
    $status         = $_POST['STATUS'];

    $strcon = mysqli_connect('localhost','root','', 'db_formacao') or die('Erro ao conectar ao banco de dados');
    $sql = "INSERT INTO participantes VALUES ('ID','$formacao', '$turma', '$colaborador', '$previsto', '$status')"; 
    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    mysqli_close($strcon);

    echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';

?> 

This is a system that is already very big so I put only the related files.

Note: It is not yet a dropdown but I will transform this input as soon as I know how to import the values from the database; If someone can show or point a way, it would be great.

    
asked by anonymous 25.08.2017 / 14:39

1 answer

2

Doing with select is basically simple. Here's a basic example

<select>
   <?php
      $sql = $pdo->prepare("SELECT * FROM tabela");
      $sql->execute();
      while($ln = $sql->fetchObject()){
         echo '<option value="'.$ln->id.'">'.$ln->nome.'</option>';
      }
   ?>
</select>

Now with Ajax

<select id="select">

</select>

$.ajax({
   url: 'URL',
   data: 'DATA',
   dataType: 'json',
   success: function(data){
      $('#select').append('<option value="'+data.id+'">'+data.nome+'</option>')
   }
})

Basically that's it.

    
25.08.2017 / 14:55