How popular is a select according to the previous select taking data from the database?

Asked

Viewed 29 times

1

So guys, I’m trying to create two selects being that one carries the specialties of doctors recorded on a table at the bench and the other shows the doctors according to each specialty marked, i am already managing to put the data of the specialties in the first select as you can see here:

  <div class="form-group col-md-6 ">
                                
    <label>Especialidades</label>
    <select name="Especialidades_idEsp" id="Especialidades_idEsp" class="form-control"  required>
      <option value="">Escolha a Especialidade</option>
          <?php
                                        
                                                      $result_esp = "SELECT * FROM  especialidades ORDER BY nome_especialidade";
                                                      $resultado_esp = mysqli_query($conexao, $result_esp);
                                                      while($row_esp = mysqli_fetch_assoc($resultado_esp)){
                                                        echo '<option value="'.$row_esp['idEsp'].'">'.$row_esp['nome_especialidade'].'</option>';
                                                      }
                                                      
                                        ?>
                                      </select> 
                                    </div>

this is my connection to the database:`<? php

$severname = "localhost"; $database = "clinicalsys"; $username = "root"; $password = "root"; //now the connection is created below $connection = mysqli_connect($severname, $username, $password, $database); if (mysqli_connect_errno()) { echo "Failed to connect to Mysql: " . mysqli_connect_error(); } ?>`

here is my file that takes the data of the names of the doctors of the database and compares with the specialty :

<?php 

    include 'conexao.php' ;

$Especialidades_idEsp = $_REQUEST['Especialidades_idEsp'];

$result_med = "SELECT * FROM medico WHERE Especialidades_idEsp=$idEsp ORDER BY nome";
$resultado_med = mysqli_query($conexao, $result_med);

while ($row_med = mysqli_fetch_assoc($resultado_med) ) {
    $medicos_post[] = array(
        'idMedico'  => $row_med['idMedico'],
        'nome' => utf8_encode($row_med['nome']),
    );
}

echo(json_encode($medicos_post));

?>

this is my javascript script to try to show in the other select the name of the doctors

<script type="text/javascript">
    $(function(){
        $('#Especialidades_idEsp').change(function(){
            if( $(this).val() ) {
                $('#idMedico').hide();
                $('.carregando').show();
                $.getJSON('post_medicos.php?search=',{Especialidades_idEsp: $(this).val(), ajax: 'true'}, function(j){
                    var options = '<option value="">Escolha o Médico</option>'; 
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].idMedico + '">' + j[i].medicos_post + '</option>';
                    }   
                    $('#idMedico').html(options).show();
                    $('.carregando').hide();
                });
            } else {
                $('#idMedico').html('<option value="">– Escolha o Medico –</option>');
            }
        });
    });
    </script>

and finally the second select:

<div class="form-group col-md-6 ">
                                        <label>Médicos</label>
                                        <span class="carregando">Aguarde, Carregando..</span>
                                        <select name="idMedico" id="idMedico "class="form-control" required>
                                        
                                        
                                      </select> 
                                    </div>

I know it got big but someone could help me ? I can’t fix it

  • it would be recommended to create a function that receives a query (query) to be sent to the database and return the requested data according to the query (function that receives the query in String and returns the result in Array). This way, you can in PHP, or Javascript, either opt to use JSON or to read associative matrices, which would be less work and more efficient for solving the problem.

No answers

Browser other questions tagged

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