Send values from a mysql select to several inputs in php

1

I have a selectbox populated from mysql from a table with two fields (id and name). The selectbox shows the list of names. I need that when selecting an element of the select send the id to an input text and the name to another input text. For now, I have managed to pass the name value to an imput text with javascript, but I can not pass the id to the other input text.

This is the code I have.

<html>
   <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
        $(function () {
            $(document).on('change', '#mySelect', function () {
                var value = $(this).val();
                $('#myInput').val(value);
                $('#myInput2').val(value);
            });
        });
      </script>
   </head>
</html>

<?php
    require('conexion.php');
    if (!$conn->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $conn->error);
    }

    $result = $conn->query("SELECT * from business");

    //config form
    echo '<form accept-charset="utf-8" action="generarqr.php" method="post">';
    echo '<select id="mySelect">';
    echo '<option value="" disabled selected>Seleccione la empresa</option>';
    while ($row = mysqli_fetch_array($result)) {
        echo '<option value="' . $row['Nombre'] . '">' . $row['Nombre'] . '</option>';
    }
    echo '</select>';
    echo '<input id="myInput"  name="data"/>';

    echo '<select id="mySelect2">';
    echo '<option value="" disabled selected>Seleccione la empresa</option>';
    while ($row = mysqli_fetch_array($result)) {
        echo '<option value="' . $row['id'] . '">' . $row['Nombre'] . '</option>';
    }
    echo '</select>';
    echo '<input id="myInput2"  name="ids"/>';
    echo '<input type="submit" value="GENERATE"></form>';

?>
    
asked by jesuriva 04.12.2018 в 17:07
source

1 answer

0

If I understand what you want is to select the value plus the text, if so you need to use the text() to get the literal of the selection and as for the value or you already do it with val() .

I'll give you an example, so you can try it and adapt to what you need:

$(function () {
    $(document).on('change', '#mySelect', function () {
        var tagOption = $('option:selected', this);
        $('#myInput').val(tagOption.val());
        $('#myInput2').val(tagOption.text());
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>

<select id="mySelect">
  <option value="v1">Valor 1</option>
  <option value="v2">Valor 2</option>
  <option value="v3">Valor 3</option>
  <option value="v4">Valor 4</option>
</select>

<br><br>
Valor: <input type="text" id="myInput" />

<br><br>
Texto: <input type="text" id="myInput2" />


</body>
</html>

If you notice first a reference to the selected option is saved with:
$('option:selected', this); in this way we force to always select the correct value and not have problems when doing text() .

    
answered by 04.12.2018 в 17:31