Select code in dropdown and show description in TextBox JSON

0

Good morning,

I have a small problem to show data results of a project.

On the one hand I have a table in Mysql called WORKS, with the fields CODE and DESCRIPTION.

In HTML, I have a dropdown list with search engine, in which all the codes of works that are open are shown, and you can also search them by code. I do this with Ajax and JSON.

And finally I have a locked textbox (disabled) where I want to show the description of the selected work in the dropdown list.

Tests.html: Dropdown:

<select class="itemName form-control" style="width:500px" name="itemName" id="itemName"></select>

Textbox:

<p><input class="form-control" type="text" disabled="disabled" id="obranom" name="obranom"></p>

script:

<script type="text/javascript">
      $('.itemName').select2({
        placeholder: 'Selecciona una obra',
        ajax: {
          url: 'ajaxpro.php',
          dataType: 'json',
          delay: 250,
          processResults: function (data) {
            return {
              results: data

            };
          },
          cache: true
        }
      });
</script>

ajaxpro.php:

<?php
define (DB_USER, "*****");
define (DB_PASSWORD, "*******");
define (DB_DATABASE, "******");
define (DB_HOST, "localhost");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

$sql = "SELECT CODIGO, DESCRIPCION FROM obras 
        WHERE CODIGO LIKE '%".$_GET['q']."%' AND ESTADO='Abierto'"; 
$result = $mysqli->query($sql);

$json = [];

while($row = $result->fetch_assoc()){
     $json[] = ['id'=>$row['CODIGO'], 'text'=>$row['CODIGO']];

}

echo json_encode($json);


?>

I need to show the description in the textbox "obranom" of the selected code in the dropdown "itemName"

Thanks in advance and greetings

    
asked by Adri404 05.06.2017 в 16:32
source

1 answer

0
var texto= $("#itemName:selected").val();
$("#obranom").val(texto);
    
answered by 05.06.2017 в 17:38