In pure Javascript, such as the question tag, change the select
to execute a function when the value is changed, via the event onchange
:
<select name="Distrito" size="1" width="180" class="COMBODISTCSS" id="COMBOFAB" tabindex="1" onchange="mudouValor()">
Create the function mudouValor
, responsible for obtaining the value of the label:
function mudouValor() {
//instancia um elemento do DOM através do respectivo id, que nesse caso é "COMBOFAB"
var elemento = document.getElementById('COMBOFAB');
//com o elemento instanciado, é possível capturar o valor da label
var texto = elemento.options[elemento.selectedIndex].innerHTML;
}
Directly with PHP will not be possible. However, you can use Javascript to fill in a field of type hidden
in your form, for example:
<form name="myform" id="myform" action="teste.php" method="POST">
<span class="IWLABEL11CSS" id="IWLABEL7">Órgão: </span>
<select name="Distrito" size="1" width="180" class="COMBODISTCSS" id="COMBOFAB" tabindex="1" onchange="mudouValor()">
<option value="01">Gabinete do Prefeito</option>
<option value="02">Coordenadoria de Governo e Comunicação Social</option>
<option value="03">Secertaria Municipal de Planejamento</option>
<option value="04">Procuradoria Jurídica</option>
<option value="05">Ouvidoria Municipal</option>
<option value="06">Secretaria Municipal de Administração</option>
</select>
<input type="hidden" name="label" id="label" />
</form>
Note that the type field was added at the end of the form hidden
with the value
filled with the value of the first option of select
:
<input type="hidden" name="label" id="label" value="Gabinete do Prefeito" />
The problem of manually initializing the field label
with the value corresponding to the first element of select
is that if this value changes, the programmer also needs to change the value
country label
and surely someone will forget that in the future, so we can automate that part:
window.onload = function(){
mudouValor();
}
The Javascript code would look like this:
function mudouValor() {
var elemento = document.getElementById('COMBOFAB');
var texto = elemento.options[elemento.selectedIndex].innerHTML;
//altera o valor do campo cujo o id seja igual a "label"
document.getElementById("label").value = texto;
}
window.onload = function(){
mudouValor();
}
Note that for the example, the form is with the attribute action
defined with the value teste.php
and with the attribute method
with the value POST
, that is, a POST Request will be made for the file teste.php
:
<form name="myform" id="myform" action="teste.php" method="POST">
With this just create the file teste.php
:
<?php
//Através da variável global "$_POST" é possível obter os valores do seu formulário.
//Cada index do array "$_POST" representa um campo do formulário, ou seja, no formulário existe um campo com o atributo name="Distrito" e outro campo com o atributo name="label"
$distrito = $_POST['Distrito'];
$label= $_POST['label'];
//...
?>
Change the names as needed, the names above have been defined for example only.
Why did you use the tag
PHP
on the question? Do you want to know how this is done with PHP (although I’m not only good with PHP) or Javascriot?– Filipe Moraes
in any of these I could use, because it is a web system that uses these technologies
– V.Avancini