How do I get the selected data from a combo?


My problem is that I do not know how to capture the selected data from the combobox list, I do not mean the value 1,2,3,4 I mean what the combo shows, in my case team names, the data I bring them with js, and I insert the numeric value that I selected from the list, and that's fine, but I also need to get and insert the team name, not just the value.

From here I extract the data, filtered by a department id.

$query = $conexion->query("SELECT * FROM equipos WHERE idDepto = $depto");

echo '<option value="0">Seleccione</option>';

while ($row = $query->fetch_assoc()) {
echo '<option value="' . $row['idEquipo'] . '">' . $row['Nombre'] . 
'</option>' . "\n";

With this function Js I pass them to HTML, where in HTML I only take "cmbequipos".


// Lista de Deptos
$.post( 'departamentos.php' ).done( function(respuesta)
    $( '#cmbdeptos' ).html( respuesta );

// lista de Deptos
    var el_continente = $(this).val();

    // Lista de Eqs
    $.post( 'equipos.php', { continente: el_continente} ).done( function( respuesta )
        $( '#cmbequipos' ).html( respuesta );

// Lista de Equipos
$( '#cmbequipos' ).change( function()
    var pais = $(this).children('option:selected').html();



HTML Vista

<select name="cmbequipos" class="form-control input-sm" required="Yes" 
id="cmbequipos" <?php echo $_SESSION["status"]; ?>>

At the moment of inserting I put something like that $var = $_POST['cmbequipos']; but I insert the value in this case the id of the Team. But I try to take the name instead of the value.

asked by Jonathan 09.08.2017 в 05:31

2 answers


I managed to solve it, since it only inserted the id value, I decided that at the time of inserting I made a query and it would bring me the name of the team since I inserted the id only, it's something like that.

 //Consulta para seleccionar los valores de la bd
$sql = "SELECT Nombre FROM equipos WHERE idEquipo = " .$_POST['cmbequipos']."";
$result = $conn->query($sql);

//contador de los valores extraidos en la consulta

if ($result->num_rows > 0) {
    // Datos obtenidos de la consulta

    while($row = $result->fetch_assoc()) 

        $NomEq = $row["Nombre"]; //Dato base requerido 
        $datos_insertar[54] = $NomEq;

answered by 09.08.2017 / 14:59

Try this

$('#cmbequipos').change(function() {
    var pais = $('#cmbequipos option:selected').html();
answered by 09.08.2017 в 06:26