4
I’ll try to get as much detail as I can about my problem. I have an HTML select that pulls your options directly from the database, as per the code below:
$conn = new mysqli('localhost', 'root', '', 'sindigrejinha') or die ('Falha ao conectar-se com DB');
$result = $conn->query("select idTransportador, Nome_Uso from transportador");
echo "<select id='listaTransp' onchange='alimentarCampo();' class='form-control' name='idTransportador'>";
while ($row = $result->fetch_assoc()) {
unset($idTransportador, $Nome_Uso);
$idTransportador = $row['idTransportador'];
$Nome_Uso = $row['Nome_Uso'];
echo '<option value="'.$idTransportador.'">'.$Nome_Uso.'</option>';
}
echo "</select>";
Depending on the option chosen, the value of an input is changed using the codes below.
Input:
echo '<input id="inputMotor" class="form-control">';
Javascript:
<script type="text/javascript">
function alimentarCampo() {
var listaTransp = document.getElementById("listaTransp");
document.getElementById("inputMotor").value = listaTransp.options[listaTransp.selectedIndex].value;
}
</script>
So far so good. The problem, is that the value that is sent to the input is that of the "idTransporter", and it would need to be of the "Conductor Name" which in the case is another attribute of the table "carrier". I NAY I can change the value of the options to $Driver Name, as they will be used in a form on another page (this is not relevant).
The question is: How can I, using the value of "idTransporter", show the respective "Conductor Name" inside the input?
Nome_Condutor
is theNome_Uso
?– bfavaretto
No. 'Use_name' is another attribute of the table that will be shown in the select options, but with that it is all right.
– valkyrieQNTUM
But this
Nome_Condutor
is not being selected in the query. Where it comes from?– bfavaretto
It is not being selected in the query, really, but it is an attribute of the table 'carrier' as well. I need to give a "nickname" (which would be the driver name) to the idTransporter and show it in the input.
– valkyrieQNTUM
So you can include this column in SELECT, and use what I suggested in the answer below.
– bfavaretto