0
I developed a PHP code that queries a database table and inserts the values into one select
in HTML.
Once the user does not find the desired option within this select
, he must select the option "Instituição não encontrada"
and then a label (id="lblCadastrarInstituicao")
and a input (id="cadastrarInstituicao")
should no longer be hidden. If the user selects the option "Instituição não encontrada"
and then switch to another option, the label
and the input
that I mentioned should return to the hidden mode.
I have a similar function on another page and it works normally, the only difference is that the other page does not make this database query. On this page the code is working in different ways in different browsers, even though I am not storing cache.
In Mozilla Firefox, the system removes the label
in the hidden way, but the input
no. If I change to a different option than "Instituição não encontrada"
, the label
does not return to hidden mode.
In Google Chrome, the system is not taking out even the label
nor the input
hidden mode, independent of the selected option.
I checked several publications of Stack Overflow PT and other sites to understand what is wrong, but I could not identify the problem.
The Javascript function follows below:
function cadastrarInstituicao() {
//Abre a função que mostra o campo CadastrarInstituicao se a opção "Instituição não encontrada" no select instituicao.
var instituicao = document.getElementById("instituicao").value;
if (instituicao === 'novaInstituicao') {
document.getElementById("cadastrarInstituicao").style.display = "block";
document.getElementById("lblCadastrarInstituicao").style.display = "block";
} else {
document.getElementById("cadastrarInstituicao").style.display = "none";
document.getElementById("lblCadastrarInstituicao").style.display = "none";
}
}//Fecha a função
PHP/HTML code:
<label for="instituicao">A unidade pertence a qual instituição?</label>
<select name="instituicao" id="instituicao">
<!-- Transforma o valor da variável $instituicao em um array e armazena esse array na variável $while. Será criado um option para cada registro na tabela instituicao. -->
<?php while($while = $instituicao->fetch_array()) { ?>
<option value="<?php echo $while["id_instituicao"]?>" onclick="cadastrarInstituicao();"> <?php echo $while["nome"]?> </option>
<?php }?>
<!-- Fazer com que ao selecionar "Cadastrar nova Instituição" seja ativada a função cadastrarInstituicao().-->
<option id="novaInstituicao" value="novaInstituicao" onclick="cadastrarInstituicao();">Instituição não encontrada</option>
</select><br>
<label id="lblCadastrarInstituicao" for="CadastrarInstituicao" style="display: none;">Cadastrar Instituição</label>
<input type="text" name="cadastrarInstituicao" id="cadastrarInstituicao" style="display: none;"><br><br>
Instead of using "block" in the display value of the label and input, use "inline". the default value for these tags is inline, using block, you must be breaking your page layout, so the correct components do not appear.
– Júlio Neto
Julio, I switched the block for inline in the function, as you suggest. Continues with the same result, except that now Mozilla is also not showing the label.
– Hugo Guitti
Another thing, that I just saw, take the onclick of the option, in place put an onchange in select, with the same content. onclick is more for buttons and links, when using a select, use onchange.
– Júlio Neto
Julio, I made the changes you suggested and now when I select some value from within select, firebug says that registering Stitution is not a function. This occurs with the function inside or outside of Document.ready. I have already checked and the JS file containing the function is correctly inserted on the page by a script tag.
– Hugo Guitti