How to dropdown to import database options

Asked

Viewed 889 times

0

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

To enroll the student in the class, I would like to have a dropdown in which I could select one of the enrolled students, I thought it could be something simple using select, but searching in forums, I didn’t understand how to do this.

The form to enroll students (collaborators) in 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 students are registered (employees):

<?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 a dropdown yet but I will transform this input as soon as I know how to import the values from the bank; If anyone can show or point a way, it would be great.

  • 1

    try to post the code you’ve already done, so it’s easier for us to help.

  • Show your code you must, young padawan. Know your context the developer needs.

  • This was even a justification that I was going to give. Registration, consultation, everything is working fine. Except what I have from this dropdawn is the bank and the form .

  • shows the database and the form to get started

1 answer

2

Making 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.

  • So, I did with your select, the problem is that in addition to not showing the bank options, everything that had below the select is gone from the page. I’ll edit my code for you to see.

  • @Marianaferreira The problem was that Voce used my code to risca, being that I used PDO and Voce used MYSQLI, I’ll edit my code to get better

  • Ah, right, all right :)

Browser other questions tagged

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