hello I have the following code so far trying to iterate X form to later fill that form with data that I already get from my database with the following format '41||THIS IS WITH PROVIDER AND WITH IMAGE||10||1 ||10||||'
const forms = document.getElementById(nombredelformulario).elements;
console.log(forms)
const form = forms[0];
[...form].forEach((input) => {
console.log(input);
});
but I can't get it on the console, it throws me the following error
funciojes.js:580 Uncaught TypeError: form is not iterable
here is the form:
<form id="form_para_editar_productos" enctype="multipart/form-data">
<input type="hidden" name="id_de_producto" id="id_de_producto">
<label>Nombre Del Producto</label>
<input type="text" name="nombre_producto_editar" id="nombre_producto_editar" class="form-control input-sm modales" onkeypress="return validar(event)">
<label>Existencias Del Producto</label>
<input type="number" name="cantidad_producto_editar" id="cantidad_producto_editar" class="form-control input-sm modales" onkeypress="return validar(event)">
<label>Unidad De Medida</label>
<select id="unidadmedida_editar" name="unidadmedida_editar" class="form-control form-select form-select-sm input-sm">
<?php
$sql="SELECT * FROM `unidadmedida`";
$result=mysqli_query($conexion,$sql);
while($ver=mysqli_fetch_row($result)):
?>
<option value="<?php echo $ver[0]?>">
<?php echo $ver[1];?>
</option>
<?php endwhile;?>
</select>
<label>Precio Del Producto</label>
<input type="number" name="precio_producto_editar" id="precio_producto_editar" class="form-control input-sm " onkeypress="return validar(event)">
<label>Proveedor</label>
<select name="provedor_editar" id="provedor_editar" class="form-control form-select form-select-sm input-sm">
<?php
$sqlproveU="SELECT * FROM `alma2`.`provedores`";
$resulU=mysqli_query($conexion,$sqlproveU);
while($ver2=mysqli_fetch_row($resulU)):
?>
<option value="<?php echo $ver2[0]?>">
<?php echo $ver2[1];?>
</option>
<?php endwhile;?>
</select>
<label>Imagen Del Producto</label>
<input type="file" id="img_producto_editar" name="img_producto_editar" class="form-control">
</form>
If what you want is to capture the nodes of the form you can use
document.querySelectorAll
and some CSS attribute that serves as a common selector for all the fields, for example using the attributename
:Or using a CSS class:
[OLD]
If you mean to get the value of the data that is entered in the form, one of the easiest options is to use
FormData
: