Select by default, depending on the value returned in MySQL?

0

Good afternoon I have the following code:

        <div class="form-group">
          <label for="exampleInputEmail1">Centro de formación: </label>
          <select class="form-control" name="centroformacion" selected="Centro de Electricidad y Automatización Industrial">
            <option value="Centro Agropecuario de Buga">Centro Agropecuario de Buga</option>
            <option value="Centro Latinoamericano de Especies Menores">Centro Latinoamericano de Especies Menores</option>
            <option value="Centro de Electricidad y Automatización Industrial">Centro de Electricidad y Automatización Industrial</option>
            <option value="Centro de la Construcción">Centro de la Construcción</option>
            <option value="Centro de Diseño Tecnológico Industrial">Centro de Diseño Tecnológico Industrial</option>
            <option value="Centro Nacional de Asistencia Técnica  a la Industria - Astin">Centro Nacional de Asistencia Técnica  a la Industria - Astin</option>
            <option value="Centro de Gestión Tecnológica de Servicios">Centro de Gestión Tecnológica de Servicios</option>
            <option value="Centro de Tecnologías Agroindustriales">Centro de Tecnologías Agroindustriales</option>
            <option value="Centro de Biotecnología Industrial">Centro de Biotecnología Industrial</option>
          </select>
        </div>

How do I default one of them, depending on what I previously stored at the time of registration?

    
asked by Andrés J Valencia 18.12.2017 в 21:06
source

1 answer

0

see if you put the select an id, example <select id="mi_select"></select> with the options inside at the time of saving the form would keep the selected option, and then when the content would show you would check which option you had selected and would ask for the content for that option, it would be like that, in the question of the example that I present to you (using jquery to make it easier):

var cual_seleccion=""
$('mi_select').change(function{  //con este evento actualizas valor cada vez que se seleccionan los datos
cual_seleccion = $('mi_select').val()  // almacenas en cual_seleccion el option del select

})

the variable which_selection is the one that you keep, since it is the one that tells you which option was selected to send to the database, and then depending on this data, suppose that in a variable data comes the data that you will put in a <p id="texto">

if(data == "Opción 1"){
  $('#texto').empty()  // para quitar lo que aya anteriormente
  $('#texto').text("aquí dentro pones la información que saldrá en el p si es Opción 1")
}
else if(data == "Opción 2"){ ...  } 

If you want a default value depending on what you have to show, it would be like this:

suppose you have this select

 <select name="" id="sele"> 
   <option  value="a">A</option>
   <option value="b">B</option>
   <option value="c">C</option>
 </select>

in javascript would say:

if(datos == "A"){  // datos es la información de mysql del servidor
    $("#sele").val('a')  
      // a es el id de la opción que deseo mostrar en el select
     // para poner los datos en alguna etiqueta te lo explique anteriormente
}
else if(datos == "B"){ $("#sele").val('b') } // igual b es el id de la opción
    
answered by 18.12.2017 в 21:22