I comment, I have a normal registration form in html, the thing is that using the select, I want to load the same value (in this case a select, when I register I select the option I want) that is stored in the DB . The id that I use in the options are 1,2,3, etc., so when I save in the DB, only the number is valid and I send a string to the field I want to save (the type of value that I sent to the DB it's a string).
To show them the selection would be this example:
<select class="form-control" name="tipo" id="tipo" required>
<option value="1">Valor 1</option>
<option value="2">Valor 1</option>
<option value="3">Valor 1</option>
</select>
Now, everything normal, I send the ajax to the php and here I validate the number and set a string for the field where I want to save it, example:
$tipo_aux = isset($_POST["tipo"])? limpiarCadena($_POST["tipo"]):"";
//Valido
if($tipo_aux=="1"){ $tipo="Opcion 1"; }
if($tipo_aux=="2"){ $tipo="Opcion 2"; } //y asi sucesivamente
//Luego aqui llamo la instancia, funcion y envio el objeto.
//Luego todo bien guardado
Now, the thing is that as I use the value of a number, when I want to edit the X record, the selectpicker does not load the option that is saved by default, but in the formulator of the option option select appears without selecting anything and you have to select it again if we want to do some editing of a field to save, otherwise the form will validate me that this selection has not been selected at all.
This is the javascript function that takes the information and sets the id of the editing form, so that they load the corresponding information consulted.
function viewItem(id){
$.post("../ajax/supplier.php?action=show",{id: id}, function(data,status){
data = JSON.parse(data);
showForm(true);
$("#id_1").val(data.Data1); //estos son ejemplos
$("#id_2").val(data.Data2);
//aqui cargo mas bla bla blaaa de los campos
//mas bla bla blaaa .....
$("#tipo").val(data.Tipo); //Aqui es donde envio al id del select, el
//valor que quiero cargar pero como el valor en la DB es un string
//(varchar) y las opciones tienen un value de tipo 1,2,3,.. no me sale
//por defecto la que esta guardada, sino que sale sin estar
//seleccionada, pero si yo lo hago con solo numeros ahi si me sale bien
//saleccionada ya que son la misma cadena de valores.
})
}
Now if someone gives me a hand please, a suggestion to not have to resort to store only numbers and then through the back end be validating the number and showing something else (if it is 1 show such a string) since I do not I really like it.
Greetings.