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

Asked

Viewed 545 times

0

Good morning Programmers.. I need a favor, next I have a combobox that is generated through a select so far 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, that is, I use this item selected in a select... the business is the following has several companies with various branches each, when the user selects his company, I want that in the next combobox appear the affiliates of the selected company, ta everything going in the conforms only I have to save this information selected... follows 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> 

In the 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 called follow 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 you need to assign that item no 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 cod_company, I need to use the selected item.. if someone intended and can help me thank you

1 answer

1


Instead of using windows.Location you should use one 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 option Select an if to validate if the value of the Select as in this example https://jsfiddle.net/6k3hh598/2/

referring to your php function you must 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 be 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

  }
}

Browser other questions tagged

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