I have a little doubt about how to fill them by obtaining the data from the server.
I have an index.html:
<form name="frmListadoPiezas3" id="frmListadoPiezas3" class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>Listado de piezas de un producto Radio</legend>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="lstProductosListado3">Producto</label>
<div class="col-md-4">
<input type="Radio" id="lstProductosListado3" name="lstProductosListado3" class="form-control">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="btnListar"></label>
<div class="col-md-4">
<input type="button" id="btnListar3" name="btnListar3" class="btn btn-success" value="Listar"></input>
</div>
</div>
</fieldset>
</form>
And an index.js:
function procesarRellenarProductos(listadoProductos)
{
console.log(listadoProductos);
$("#lstProductosListado3").empty();
let sOption="";
for(let i=0;i<listadoProductos.length;i++)
{
sOption += "<option value='"+listadoProductos[i].idproducto+"'>"+listadoProductos[i].nombre+"</option>"
}
$("#lstProductosListado").html(sOption);
}
The processFillProducts function as it is right now fills a dropdown that works correctly, but when I want it to fill a radio and a check (using a function for each one) I don't know how I should do it inside the for. I have tried several things and it does not show me anything, only 1 empty radius circle. I do the filling through ajax calling a php file where I have the connection and consult the server.
This is the part not to wear:
sOption += "<option value='"+listadoProductos[i].idproducto+"'>"+listadoProductos[i].nombre+"</option>"
I hope you can help me, thanks.
I add the functions that call the server:
function rellenarCombosProductos()
{
var iListaProductos = null;
if(localStorage["nombreProductos"] != null)
{
iListaProductos = JSON.parse(localStorage["nombreProductos"]);
procesarRellenarComboProductos(iListaProductos);
}
else
{
let oAjax = instanciarXHR();
var sURL = "./php/getProductos.php";
oAjax.open("GET", encodeURI(sURL));
oAjax.addEventListener("readystatechange", procesarRespuesta);
oAjax.send();
}
}
function procesarRespuesta()
{
var oAjax = this;
//console.log(oAjax.responseText)
if (oAjax.readyState == 4 && oAjax.status == 200)
{
let listadoProductos = JSON.parse(oAjax.responseText);
console.log(listadoProductos)
procesarRellenarComboProductos(listadoProductos);
localStorage["nombreProductos"] = JSON.stringify(listadoProductos);
}
}
function instanciarXHR() {
var xhttp = null;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else // code for IE5 and IE6
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhttp;
}
Without testing any of your code I see several things that are not entirely correct, such as adding an option element to an input, the option element has to be explicitly the child of the select element.
Apart from this I see another thing, you are supposedly trying to create a radio button, because for this you would have to use inputs, you have to use the type property with the radio value, that is type="radio"
Here an example: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio
You have the error in this line:
Why ? Well, because I can't see any element of the html that you have passed with this id, the closest one and I suppose you mean this: #lstProductosListado3
Apart from all this what I have told you, comment that for the radios to be exclusive, you have to give them the same name.
So in the "sOption +=" that you have in the for loop, put the following:
Also comment that it is not a good practice to use the html() function because if, for example, you already have child elements in the element where you use this function, it will eliminate them, I recommend using the append or appendTo function .
You use the list of products as an array which is a bit weird, I would do the following:
In the case of the checkbox it is similar, here I give you the syntax:
https://developer.mozilla.org/en/docs/Web/HTML/Element/input/checkbox
It would be the same as before, concatenate it as you see it, referring to the example you use with the html() method
Finally comment that I suppose you have tested that the return of data is correct via the debugger, anyway, I recommend you to use the $.ajax() function, if you want to use my example on the data addition, adapt it to your Project.
I'm going to leave you the HTML and the JS so that you can better see how to adapt it, also remember to use jquery locally, you would have to change the src of the script element.
HTML: https://pastebin.com/MrAeawm8
JS: https://pastebin.com/8b1XymzK
The latter is the one of good practice.
To begin with you are introducing with the hmtl() method where it is not:
Then with the html() method: