Pass Javascript variable to PHP body

Asked

Viewed 1,007 times

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 the Nome_Uso?

  • No. 'Use_name' is another attribute of the table that will be shown in the select options, but with that it is all right.

  • But this Nome_Condutor is not being selected in the query. Where it comes from?

  • 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.

  • So you can include this column in SELECT, and use what I suggested in the answer below.

1 answer

5


If it is possible to bring the name of the driver in the same query, I would put it inside a data attribute:

while ($row = $result->fetch_assoc()) {
    unset($idTransportador, $Nome_Uso);
    $idTransportador = $row['idTransportador'];
    $Nome_Uso = $row['Nome_Uso']; 
    $Nome_Condutor = $row['Nome_Condutor']; 
    echo '<option value="'.$idTransportador.'" data-nome-condutor="'.$Nome_Condutor.'">'.$Nome_Uso.'</option>';
}

There in the JS just do so:

function alimentarCampo() {
    var listaTransp = document.getElementById("listaTransp");
    document.getElementById("inputMotor").value = listaTransp.options[listaTransp.selectedIndex].getAttribute('data-nome-condutor');
}
  • It worked perfectly, thank you very much.

  • Good, you are welcome!

Browser other questions tagged

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