Help Dynamic Combo with javascript

Asked

Viewed 560 times

0

I am doing some tests with PHP + Oracle and I have a doubt. I made a combo, which takes data from Oracle, and that controls a textfield. Only now I need to update one more textfield, but there is no way. Someone can help me?

NOTE: In this text in the code the description is normal, but I need to add the "sector" in another.....

Javascript

function alimentarCampo() {
        var codCampo = document.getElementById("codCampo");
        document.getElementById("descrCampo").value = codCampo.options[codCampo.selectedIndex].value;
}

PHP

<?php
  //Inicia seleção Combo e descreve no Text (Maquinas)
?>
             <tr>
                 <td>Máquina:</td>
                 <td><select id="codCampo" name="codMaquinas" onchange="alimentarCampo();">
                 <option></option>
             <?
                 include('config.php'); //conexao com o banco
                 //monta dados do combo das maquinas
                 $consulta = OCIParse($ora_conexao,"select CODIGO,NOME,SETOR from pcn_manut_maquina
                                                    order by CODIGO");
                 OCIDefineByName($consulta,"CODIGO",$v_num);
                 OCIDefineByName($consulta,"NOME",$v_nome);
                 OCIDefineByName($consulta,"SETOR",$v_setor);
                 OCIExecute($consulta);
                     while (OCIFetch($consulta)){
                        echo "<option value=\"".$v_nome."\">".$v_num."</option>";  //PRECISA DAS \ PARA PEGAR CAMPOS COM ESPAÇO
                     }
                        echo "</td>";

             ?>
            </tr>

              <tr>
                 <td></td>
                 <td><input type="text" id="descrCampo" name="descrMaquinas" readonly="true" size="60"></td>
              </tr>
  • This smells very expensive to me of Ajax..... I think it would make your life easier

1 answer

2


An alternative is in PHP to put the name + sector in VALUE:

echo "<option value=\"".$v_nome."|" . $v_setor . "\">".$v_num."</option>";

and in Javascript,

    function alimentarCampo() {
            var codCampo = var x = document.getElementById("codCampo").selectedIndex;
            var campos = codCampo.split("|");
            document.getElementById("descrCampo").value = campos[0];
            document.getElementById("setor").value = campos[1];
    }

Browser other questions tagged

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