Asigar Value obtained from a Database to a SELECT

1

Good attempt to Assign the Value obtained from a query to a Database to a select

Query in Database

    <?php
       $lsq="SELECT DISTINCT opcion FROM tabla1";
       $quer = mysqli_query($con, $lsq);
         while ($raw=mysqli_fetch_array($quer)){
          $opcion=$raw['opcion'];
 <input type="hidden" id="opcion<?php echo $opcion; ?>"  value="<?php echo $opcion;?>" >
        }
    ?>
     <td class='text-center'><a class='btn btn-info' href="#" onclick="enviar('<?php echo $numero_doc ?>')"><i class="glyphicon glyphicon-plus"></i></a></td>

This is an anchor that when pressed will get the value (option) will call me the next function

 <a class='btn btn-info' href="#" onclick="enviar('<?php echo $opcion ?>')"><i class="glyphicon glyphicon-plus"></i></a>

Funcio.JS

function enviar(id)
{   
    var opcion= $("#opcion"+id).val();

    $("#selesct").val(opcion);
}

In that function I receive the value (option) and assign it to the id of the select

Select that you should receive the value and change according to the received value

        <div class="col-md-2">
           <select class='form-control input-sm' id="selesct" name="selesct" readonly>
            <option value="">..Seleccione..</option>  
            <option value="1">CONTADO</option>
            <option value="2">CREDITO</option>                                
           </select>
        </div>

if the value is 1 the select should show COUNTY , if it is 2 CREDIT

    
asked by David 20.08.2017 в 18:09
source

1 answer

0

You could do this way:

    function enviar(id) {
        var opcion = $("#opcion" + id).val();

        $('#selesct option[value="'+ opcion + '"]').prop('selected', true);
    }

Depending on your version of jQuery, it could be used differently:

$('#selesct option[value="'+ opcion + '"]').attr('selected', 'selected');
    
answered by 21.08.2017 / 21:16
source