I need help with several things:
First, I want to know how to get the id of an input, if it comes dynamically from the database. In the html it shows me for example for field 1 -> id = "parameter1"
, for field 2 -> id = "parameter2"
<input id='parametro"+ index.id +"' value='" + index.id + "'></input>
This is the jQuery code to fetch all the elements from the table and display them in the html. Second we have the update button, what you have to do is grab the data and update them all together, I know that I must do a forEach to do all the updates, but I don’t know how to do it or where to accommodate it.
$(document).ready(function () {
function getParametros() {
$.ajax({
url: "http://localhost/slim/apiParametros.php/db_parametros",
type: "get",
success: function (response) {
$(".primerSemestre").html('');
$(".segundoSemestre").html('');
$.each(JSON.parse(response), function (i, index) {
if (index.id <= 6) {
$(".primerSemestre").append("<div class='input-group'>" +
"<input id='parametro"+ index.id +"' value='" + index.id + "' hidden></input>"+
"<div id='parametro"+ index.mes +"' class='input-group-prepend' style='flex:0 0 20%'><span class='input-group-text w-100'>" + index.mes + "</span></div>" +
"<input type='number' id='parametro"+ index.valor +"' class='form-control' value='" + index.valor + "'>" +
"</div><br>");
}
if (index.id > 6) {
$(".segundoSemestre").append("<div class='input-group'>" +
"<input id='parametro"+ index.id +"' value='" + index.id + "' hidden></input>"+
"<div id='parametro"+ index.mes +"' class='input-group-append' style='flex:0 0 20%'><span class='input-group-text w-100'>" + index.mes + "</span></div>" +
"<input type='number' id='parametro"+ index.valor +"' class='form-control' value='" + index.valor + "'>" +
"</div><br>");
}
});
}
});
}
getParametros();
$("#btnGuardar").click(function () {
var id = $('#parametro'+'2').val();
console.log(id)
var mes = $('#parametro'+'Febrero').text();
console.log(mes)
var valor = $('#parametro'+'20').val();
console.log(valor)
$.ajax({
url: "http://localhost/slim/apiParametros.php/db_parametros/" + id,
type: "put",
data: { id: id, mes: mes, valor: valor },
success: function () {
getParametros();
}
});
});
});
2
Answers
store data in JSON > when function fires > update values as per Key and values
You can add
class
to each inputs then it would be easy to identify them . So , whenever yourbtnGuardar
is clicked you can simply use.each
to iterate through your inputs and then get values of input using$(this).find(".yourclassname").val()
and pass this to your backendDemo Code :