I am developing a crud in php, with ajax and js
When I register a record in the corresponding modal, I have a select in which I indicate the category to which it belongs, which stores the selected option in the status table.
My problem comes when I want to edit a record, the select brings it to me empty . How should I do so that, when editing the registry, the select shows the option that I selected at the time the registry was registered.
I hope you can understand me, I share the code where I call the edit function.
//Editar
$(document).on("click", ".btnEditar", function () {
opcion = 2;//editar
fila = $(this).closest("tr");
id = parseInt(fila.find('td:eq(0)').text()); //capturo el ID
folio = fila.find('td:eq(1)').text();
nombre = fila.find('td:eq(2)').text();
apellido = fila.find('td:eq(3)').text();
direccion = fila.find('td:eq(4)').text();
descripcion_domicilio = fila.find('td:eq(5)').text();
telefono = fila.find('td:eq(6)').text();
num_contrato = fila.find('td:eq(7)').text();
fecha_inicio = fila.find('td:eq(8)').text();
fecha_final = fila.find('td:eq(9)').text();
cuadrilla = fila.find('td:eq(10)').text();
estatus = fila.find('td:eq(11)').text();
console.log(estatus)
observaciones = fila.find('td:eq(12)').text();
encargado_cuadrilla = fila.find('td:eq(13)').text();
otra_descripcion = fila.find('td:eq(14)').text();
$("#folio").val(folio);
$("#nombre").val(nombre);
$("#apellido").val(apellido);
$("#direccion").val(direccion);
$("#descripcion_domicilio").val(descripcion_domicilio);
$("#telefono").val(telefono);
$("#num_contrato").val(num_contrato);
$("#fecha_inicio").val(fecha_inicio);
$("#fecha_final").val(fecha_final);
$("#cuadrilla").val(cuadrilla);
$("#estatus").val(estatus);
$("#observaciones").val(observaciones);
$("#encargado_cuadrilla").val(encargado_cuadrilla);
Here I show part of the code, which is inside the modal where I show the categories to which the select belongs
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="" class="col-form-label">Estatus</label>
<select name="status" id="estatus" class="form-control" required>
<option selected>eleccione una opción</option>
<option value="1">Pendiente</option>
<option value="2">En Proceso</option>
<option value="3">Terminado</option>
</select>
</div>
You can use this:
where:
estatus
.Here is an example snippet:
To be able to set the entry saved in the database you have to work with the
id
element, for examplewhere
value
it should be theid
item in the database. For example , itid=1
would correspond to pending in my database. In summary, to work with select you only have to use the id... You are doing it in the correct way... but the text is not passed to the select, it is passed the value and you should have something like this$("#estatus").val(idEstatus);