Doubts with value of selects

Asked

Viewed 89 times

1

I made that form with the selects doing direct search of the database and also sorting the data, but when sending the form answered value that goes into the <option> is not the value I want, but another value, as shown in the first field of the form "REGIONAL". The value that enters is the variable of id_regional, but I want the variable regional.

Note: To help better understand how this part of the code works https://www.dropbox.com/s/v34dyd920djpop6/45.zip?dl=0..

<?php
     include '/conexion.php'; 


    $query = "SELECT id_regional, regional FROM t_regional ORDER BY regional";
    $resultado=$mysqli->query($query);
?>

<html>
    <head>
        <title>ComboBox Ajax, PHP y MySQL</title>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

        <script language="javascript">
            $(document).ready(function(){
                $("#cbx_regional").change(function () {

                    $('#cbx_tipoUnidade').find('option').remove().end().append('<option value="whatever"></option>').val('whatever');

                    $("#cbx_regional option:selected").each(function () {
                        id_regional = $(this).val();
                        $.post("includes/getRPA.php", { id_regional: id_regional }, function(data){
                            $("#cbx_rpa").html(data);
                        });            
                    });
                })
            });

            $(document).ready(function(){
                $("#cbx_rpa").change(function () {
                    $("#cbx_rpa option:selected").each(function () {
                        id_rpa = $(this).val();
                        $.post("includes/getTipoUnidade.php", { id_rpa: id_rpa }, function(data){
                            $("#cbx_tipoUnidade").html(data);
                        });            
                    });
                })
            });

            $(document).ready(function(){
                $("#cbx_tipoUnidade").change(function () {
                    $("#cbx_tipoUnidade option:selected").each(function () {
                        id_tipoUnidade = $(this).val();
                        $.post("includes/getUnidade.php", { id_tipoUnidade: id_tipoUnidade }, function(data){
                            $("#cbx_Unidade").html(data);
                        });            
                    });
                })
            });

            $(document).ready(function(){
                $("#cbx_Unidade").change(function () {
                    $("#cbx_Unidade option:selected").each(function () {
                        id_unidade = $(this).val();
                        $.post("includes/getInep.php", { id_unidade: id_unidade }, function(data){
                            $("#cbx_Inep").html(data);
                        });            
                    });
                })
            });


            function populate4(verificamodalidade,resultado){
                  var verificamodalidade = document.getElementById(verificamodalidade);
                      var resultado = document.getElementById(resultado);

                        resultado.innerHTML = "";
                             if(verificamodalidade.value == "CORREÇÃO DE FLUXO"){ //verificar se o valor da modalidade de ensino
                                var optionArray = ["|","SE LIGA|SE LIGA","ACELERA|ACELERA","TRAVESSIA RECIFE|TRAVESSIA RECIFE"]; //retorna os anos de ensino
                                 } else if(verificamodalidade.value == "EDUCAÇÃO JOVENS E ADULTOS"){ //verificar se o valor da modalidade de ensino 
                                var optionArray = ["|","MÓDULO 1|MÓDULO 1","MÓDULO 2|MÓDULO 2","MÓDULO 3|MÓDULO 3","MODULADA|MODULADA"]; //retorna os anos de ensino
                                 } else if(verificamodalidade.value == "EDUCAÇÃO INFANTIL"){ //verificar se o valor da modalidade de ensino 
                                var optionArray = ["|","BERÇÁRIO|BERÇÁRIO","GRUPO I|GRUPO I","GRUPO II|GRUPO II","GRUPO III|GRUPO III","GRUPO IV|GRUPO IV","GRUPO V|GRUPO V"]; //retorna os anos de ensino
                                 } else if(verificamodalidade.value == "ENSINO FUNDAMENTAL"){ //verificar se o valor da modalidade de ensino 
                                var optionArray = ["|","1° ANO|1° ANO","2° ANO|2° ANO","3° ANO|3° ANO","4° ANO|4° ANO","5° ANO|5º ANO"]; //retorna os anos de ensino
                                 } else if(verificamodalidade.value == "ENSINO FUNDAMENTAL ESPECIAL"){ //verificar se o valor da modalidade de ensino 
                                var optionArray = ["|","1° ANO|1° ANO","2° ANO|2° ANO","3° ANO|3° ANO","4° ANO|4° ANO","5° ANO|5º ANO"]; //retorna os anos de ensino
                                 }
                                        for(var option in optionArray){
                                           var pair = optionArray[option].split("|");
                                              var newOption = document.createElement("option");
                                               newOption.value = pair[0];
                                               newOption.innerHTML = pair[1];
                                               resultado.options.add(newOption);
                                  }
                         }

        </script>

    </head>

    <body>

    <br/><br/>
<h3>Informe os dados da turma:</h3>
Os campos com <span style="color: red">*</span> são de preenchimento obrigatório!
<br/><br/><br/>
        <form id="combo" name="combo" action="guarda.php" method="POST">
            <div>Regional :<span style="color: red">*</span><br/> <select name="cbx_regional" id="cbx_regional" required >
            <option value="0"></option>
            <?php while($row = $resultado->fetch_assoc()) { ?>
            <option value="<?php echo $row['id_regional']; ?>"><?php echo $row['regional']; ?></option>
            <?php } ?>
            </select></div>
            <br/>
            <div>RPA :<span style="color: red">*</span><br/> <select name="cbx_rpa" id="cbx_rpa" required>
            </select></div>
            <br />
            <div>Tipo de Unidade :<span style="color: red">*</span><br/> 
            <select name="cbx_tipoUnidade" id="cbx_tipoUnidade" required></select></div>
            <br/>
            <div>Unidade de Ensino :<span style="color: red">*</span><br/> 
            <select name="cbx_Unidade" id="cbx_Unidade" required></select></div>
            <br/>
            <div>INEP :<span style="color: red">*</span><br/> <select name="cbx_Inep" id="cbx_Inep" required>
            </select></div>
            <br/>
            Modalidade de Ensino:<span style="color: red">*</span><br/>
            <select id="mod" name="mod" onchange="populate4(this.id,'ano')" required>
            <option value=""></option>
            <option value="CORREÇÃO DE FLUXO">CORREÇÃO DE FLUXO</option>
            <option value="EDUCAÇÃO INFANTIL">EDUCAÇÃO INFANTIL</option>
            <option value="EDUCAÇÃO JOVENS E ADULTOS">EDUCAÇÃO JOVENS E ADULTOS</option>
            <option value="ENSINO FUNDAMENTAL">ENSINO FUNDAMENTAL</option>
            <option value="ENSINO FUNDAMENTAL ESPECIAL">ENSINO FUNDAMENTAL ESPECIAL</option>
            </select>
            <br/><br/>
            Ano de Ensino:<span style="color: red">*</span><br/>
            <select id="ano" name="ano" required></select>
            <br/><br/>
            Turma:<span style="color: red">*</span><br/>
            <select id="turma" name="turma" required>
            <option value=""></option>
            <option value="A">A</option>  
            <option value="B">B</option>
            <option value="C">C</option>
            <option value="D">D</option>
            <option value="E">E</option>
            <option value="F">F</option>
            <option value="G">G</option>
            <option value="H">H</option>
            <option value="I">I</option>
            </select>
            <br/><br/>
            Turno:<span style="color: red">*</span><br/>
            <select id="turno" name="turno" required>
            <option value=""></option>
            <option value="MANHÃ">MANHÃ</option>
            <option value="TARDE">TARDE</option>
            <option value="NOITE">NOITE</option>
            <option value="INTEGRAL">INTEGRAL</option>
            </select>
            <br/><br/>
            Situação da Turma:<span style="color: red">*</span><br/>
            <select id="situacaoTurma" name="situacaoTurma" required>
            <option value=""></option>  
            <option value="CADEIRA VAGA / PROF. EFETIVO">CADEIRA VAGA / PROF. EFETIVO</option>
            <option value="CADEIRA VAGA / CTD">CADEIRA VAGA / CTD</option>
            <option value="CADEIRA VAGA / ACUMULAÇÃO">CADEIRA VAGA / ACUMULAÇÃO</option>
            <option value="CADEIRA VAGA / SEM PROFESSOR">CADEIRA VAGA / SEM PROFESSOR</option>
            </select>
            <br/><br/>          
            <input type="submit" id="enviar" name="enviar" value="Finalizar" />
        </form>
    </body>
</html>
  • First you have to resolve the situation of the question that is somewhat confused and poorly formulated.

  • I edited the question to see if we can now better understand the situation.

  • If you want the value of regional, why don’t you change id_regional for regional?

  • Because id_regional and reference of another in the table to do the screening in the other fields also this way always the id of another table comes in the field to be able to do the screening until it arrives in the last field that is the Unit.

  • I tried not to work.

  • Yes, it is difficult for you to understand if you want to look at it as funcioana https://www.dropbox.com/s/v34dyd920djpop6/45.zip?dl=0, ai vc vai ver como funciona na prática o banco esta dentro da pasta .

  • Humrum, I just don’t know if I did it right. kkkk

  • ok flw friend...

Show 3 more comments

2 answers

0

You are setting the value of your option with id_regional and not regional, as you want. Change the option line to:

<option value="<?php echo $row['regional']; ?>"><?php echo $row['regional']; ?></option>

Editing:

Since you need both information, you can concatenate the values in the option value and treat this later in PHP.

No option, do:

<option value="<?php echo $row['id_regional'] . "_" . $row['regional']; ?>"><?php echo $row['regional']; ?></option>

From there in PHP you can loop the values (or any other way you prefer) and take both information.

  • I know my friend, but I have to have this id_regional variable in the field because without it I can’t do the data screening

  • I edited the answer for a possible solution. You can put the concatenated information in value and treat this in PHP when picking up the data.

  • Take a look at how the full code works in practice https://www.dropbox.com/s/v34dyd920djpop6/45.zip?dl=0

  • I’m sorry, but what do you want me to see? Your code is the same way you put it here. If you need the two data, or concatenate or otherwise assemble, a two-dimensional array, maybe.

  • 1

    It was only for you to have an idea of how to work with it complete.. You can not concatenate, but flw ae by help friend.. I’ll see if I can solve this mess kkkk...

  • well, if you can’t concatenate, you can create a Hidden with name id_regional, then create a JS method that will be called in your select’s onchange and assign the value id_regional to Hidden. So your form will have all the information you need.

Show 1 more comment

0

In the original script of the question is already very close to a solution that I will suggest. Because the onchange event is already registered.

When the user chooses the option, assign the option label to an Hidden type field.

For example, create a hidden field

<input type="hidden" name="regional" value="">

In the event Rigger already created (on change), add only 2 lines as the example below

$("#cbx_regional").change(function () {

    // isso aqui pega o valor que você quer.
    var str = $("#cbx_regional option:selected").text(); 

    // Atribui o valor ao campo oculto.
    $("input[name=regional]").val(str );

    // Abaixo segue o script original sem alterar nada.

The advantage of this is cleaner, easier-to-maintain code.

As you yourself mentioned in the comments, the technique of concatenating the name with the id is valid but makes other functions difficult.

The hidden field can be anywhere because jquery will look for it by name and tag. Don’t worry about putting it inside the tag <form>

  • Didn’t work, buddy

  • don’t post vague comments if you want feedback

Browser other questions tagged

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