Pass the value of an option to another option without going through pages

Asked

Viewed 128 times

1

I have an option

<p> Curso:
    <select name='curso' id="aparecercurso" class="escolhercursos">
        <option>Nenhum</option>
        <? 

            $sqlcursos = "SELECT designacao FROM cursos";

            $respostacurso = mysql_query($sqlcursos,$conexao);

            while ($curso = mysql_fetch_array($respostacurso)) 
            {

            echo "<option> ".$curso['designacao']."</option>";

            }


            ?>
    </select>
</p>

And I wanted to use whatever value I chose on this option to open another one like this:

    <p> Disciplina:
                            <select name='disciplina' required tabindex="9">
                                <? 
                                $escolha = $_POST['curso'];
                                $sqltentar = "SELECT d.designacao_disciplina 
                                              FROM disciplinas d, disc_curso dc, cursos c 
                                              WHERE c.designacao = '".$escolha."'
                                              AND d.cod_disciplina = dc.cod_disciplina
                                              AND dc.n_curso = c.n_curso ";

                                $respostacurso = mysql_query($sqltentar,$conexao);

                                while ($rowcurso = mysql_fetch_array($respostacurso)) 
                                {

                                    echo "<option> ".$rowcurso['designacao_disciplina']."</option>";

                                }


                                ?>
                            </select>
                        </p>

I just don’t know how to pass the value of the first option for the next. I’ve tried how they come up through POST but I think it would only happen after clicking Ubmit. With javascript it was only take the id of the element but then how to go to PHP again?

  • 2

    We already have solution for this on the site, if you will use JS, the solution is to use AJAX making the JS request the data of a PHP

  • http://answall.com/search?q=%5Bajax%5D+%5Bphp%5D+select

  • I try to avoid always using AJAX, because I read in an article, that if I used AJAX for nothing, the site might have problems. But if you have no other suggestion than AJAX..

  • Using anything for nothing gives problems (in fact, these are the so-called "good practices", that is, things that are told and repeated without context and are useless). If you want to take data from a PHP without reloading, there is no option left but Ajax. Take advantage of the following link, which has a functional example in the answer that is exactly what you need to do: http://answall.com/questions/26291/popular-combo-com-chamada-de-ajax

  • 1

    I am making this form using bootstrap modals. With ajax updating the page the modal will not close?

  • Ajax doesn’t do anything you don’t have it do. It does the request and calls the function you point to perform when changing the state. Your job decides everything else.

Show 1 more comment

1 answer

1

Your answer goes through AJAX. Test this function:

<script>
$(function(){

    //Toda vez que o curso for selecionado. Nota: a primeira op'cao tem que ser do tipo "Escolha o seu curso" com Id=0 por exemplo
    $(document).on("change", "#aparecercurso", function(){
      var cursoID = $(this).val();
      //alert(spaID);

      if(cursoID>0){
         var dt={ 
                    curso:cursoID
                };
        //FUncao AJAX manda dados dt para a pagina minhaQuery.php e espera receber um JSON de volta
        var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
                    url: "minhaQuery.php",
                    type: "POST",
                    data: dt,
                    dataType: "json"
                    }); 

        //Se tudo se passar bem com a requisicao
            request.done(function(dataset){
                for (var index in dataset){ 
                     var seelctJS=dataset[index].select;
                 }

                 //Insira o select dentro da div
                $( "#selectCourseResp" ).html( seelctJS );
         }); 
        //Se AJAX falhar
            request.fail(function(jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            }); 

      }//End of if(cursoID>0)     
   }); //End of $(document).on("change", ...     
});//End of $(function(){
</script>    

//No seu HTML crie essa div vazia
<div id="selectCourseResp"></div>

//Do lado PHP minhaQuery.php

<?php
$str=""//Declare a variável. Se PHP estiver funcionando no modo strict vai      //gerar um warnig que pode bloquear o processo. Não é um erro mas um warning
$str .= "<select name='disciplina' required tabindex='9'>";

    $escolha = $_POST['curso'];
    $sqltentar = "SELECT d.designacao_disciplina 
                  FROM disciplinas d, disc_curso dc, cursos c 
                  WHERE c.designacao = '".$escolha."'
                  AND d.cod_disciplina = dc.cod_disciplina
                  AND dc.n_curso = c.n_curso ";

    $respostacurso = mysql_query($sqltentar,$conexao);

    while ($rowcurso = mysql_fetch_array($respostacurso)) 
    {

        $str .= "<option> ".$rowcurso['designacao_disciplina']."</option>";

    }
$str .= "</select>";
?>
$arrToJSON = array(
    "select"=>$str
    );  
return json_encode(array($arrToJSON));

Browser other questions tagged

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