How to receive the selected item from a combo box and use on another select?

0

Good morning Programmers .. I need a favor, next I have a combobox that is generated through a select until then everything ok .. I need to receive this item selected by the user and save the information because in the next combobox I use this information, ie I use this selected item in a select ... the business is the following has several companies with several branches each, when the user select the company from it, I want the next combobox to appear in the selected company, it's all going to be in compliance. I just have to save the selected information ... follow the codes:

<select size="1" name="Empresa" id="Empresa" onchange="opcao()">
                            <option selected value="Selecione">Empresa </option>                            
                            <?php
                             for($x=0;$x<count($resposta2);$x++)
                             {
                             echo '<option value= "'.$resposta2[$x][0].'">'.$resposta2[$x][1].' </option>';

                             }?>                          
                        </select>

                        <p></p>

                        <select size="1" name="Unidade">
                            <option selected value="Selecione">Unidade</option>
                                <?php
                             for($x=0;$x<count($resunid);$x++)
                             {
                             echo '<option value= "'.$resunid[$x][0].'">'.$resunid[$x][1].' </option>';
                             }?>          

                        </select> 

At first as I already have all system companies, I need that in the onchange where I call a js, be saved the selected item and use in the function call follows the js

<script type="text/javascript">
           function opcao(){
                     var x = document.getElementById("Empresa").selectedIndex;

                     var endereco = document.getElementById("Empresa").options[x].value;

                     if (endereco != "#")
                   {
                       var $unid = document.getElementById("Empresa").value;    
                             window.location = "totalpago.php?parametro=$unid"
                        alert($unid);
                   }
        </script>

After saving the selected item need to assign this item in the function below

function todasunidades()
     {
            include("conexao.php");

            if (!conectaBancoDados()) {
                    $resposta2 = "<center><b>Não foi possível estabelecer conexão com o Banco de Dados!</b></center>";
            }
            else


            {
                    $comandoSql = "SELECT Cod_UC, UC FROM Tab_UC where Cod_Empresa=";  é aqui que preciso usar o item selecionado...

                    $dados = mysql_db_query($bancoDados, $comandoSql) or die (mysql_error());
                       $x=0;
                    while ($linha = mysql_fetch_array($dados))
                    {

                            $resunid[$x][0] = $linha["Cod_UC"]; 
                            $resunid[$x][1] = $linha["UC"]; 
                            $x++;
                    }
            }

            return $resunid;
    }

and in company_code, I need to use the selected item .. if anyone tried and could help me thank you

    
asked by anonymous 10.05.2016 / 16:16

1 answer

1

Instead of using a windows.location you should use a ajax request

var xhttp = new XMLHttpRequest();
xhttp.open("GET", "totalpago.php?parametro="+$unid, true);
xhttp.send();
var unidades = xhttp.responseText;

To prevent the ajax call from being made when selecting the Select option, make an if to validate whether the value of the Select as in this example link

For your php function you should give a get of the parameter you are passing

$comandoSql = "SELECT Cod_UC, UC FROM Tab_UC where Cod_Empresa=" .$_GET['parametro]; 

the answer you return in json format

return json_encode($resunid);

Your final js script should look something like

function opcao(){
  var x = document.getElementById("Empresa").value;
  if (endereco != "")
  {
    var x= document.getElementById("Empresa").value;    

    //Chamada ajax
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "totalpago.php?parametro="+$unid, true);
    xhttp.send();
    var unidades = xhttp.responseText;

    //Aqui você deverá fazar a lógica para alimentar o select

  }
}
    
10.05.2016 / 17:14