Validate a select in JQuery by obtaining a string value from mysql

0

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.

    
asked by masonhsp 17.08.2018 в 05:09
source

1 answer

0

already several days ago I had found the solution and it was very simple, apologize to those who steal time, but hey, the solution was refreshing the id of the selectpicker.

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);
 $('#tipo').selectpicker('refresh'); //Aqui es donde hice la solucion
 //con la misma libreria se puede realizar un refresh y asi queda 
 //seleccionado el id que debe ser
})
}

That yes, this solution could only find it making another query just at the beginning of the function, when I perform the function of init in the .js file, in this way I filled the list for the input of type select start already full, what I went back to fill it and looked for the id to select it as soon as the value to be edited was selected from the whole row, so it was shown from the beginning already selected by what was not edited and pressed to save.

$.post("../ajax/supplier.php?action=listType", function(r) { 
//el parametro 'r' son las opcioes que nos esta devolviendo la funcion ajax de supplier.php
   $("#tipo").html(r);
   $('#tipo').selectpicker('refresh'); 
   //Esto es para obligar a refrescar el id del tipo para que salga al inicio la lista
});

Greetings.

    
answered by 30.08.2018 в 06:26