Display data of a selecct with JQuery, if the value comes from MySql

-2

How to show the selected option of a select with JQuery, in a form at the moment of modifying the data, if the value of the selected object comes from MySql

This is my php code ...

  <div class="form-group" id="selectRama">
      <label for="rama">Rama</label>
                    <?php
                    //Consultar la base de datos
                    $consultar_mysql_rama = "SELECT * FROM RAMA";
                    $resultadoRama = mysqli_query($conexion, $consultar_mysql_rama);
                    ?>
        <select id="rama" name="rama" class="form-control" required>
          <option value="">Seleccione la rama</option>
                        <?php while ($lista=mysqli_fetch_assoc($resultadoRama)) { ?>
                            <option value="<?php echo $lista['RAMA_ID']?>"><?php echo $lista=utf8_encode($lista['NOMBRE'])?></option>
                        <?php } ?>
        </select>
    </div>

And with JQuery this is what I'm doing ...

$('#rama').html(rama);

But we do not know the value selected in the form when modifying ...

    
asked by user47513 13.06.2017 в 20:59
source

2 answers

0

I agree with the friend above anyway, I'll give you one more example corriedno

$('#valores').change(function(){
    alert('La opción seleccionada es :  '+$('#valores').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='valores'>
  <option id='opt1'>Ivan</option>
  <option id='opt2'>Joel</option>
  <option id='opt3'>Jose</option>
</select>
    
answered by 13.06.2017 в 21:15
0

I suppose that your code generates an HTML structure similar to this one:

<select id='valores'>
  <option id='opt1'>1</option>
  <option id='opt2'>2</option>
  <option id='opt3'>3</option>
</select>

If so, the JS code you need is:

$('#valores').change(function(){
    alert($('#valores').val());
});

Obviously it will only give you the value when you change the selection, but basically the only thing you have to do to get the value of the select is:

$('#valores).val();

If you want the text that has the selection, it would be:

$('#valores').find(':selected').text();

link

If you have any comments, Luck.

Update

Okay, now that I see the html block that generates your code my recommendation would be that when you finish loading your page, you assign the id id or your tags <option></option>

You can do it with the following code:

$(document).ready(function(){
   $('#idSelect').find('option').each(function(i){
        $(this).attr('id','opt'+i);
    });
});

This will assign an incremental id to your options and you can manipulate them with a simple for in js cycle.

If you have another question, comment. Good luck

    
answered by 13.06.2017 в 21:09