I have an order form with several input and I want to send the value of all these input to a servlet through jQery ajax to a servlet, my problem is that the amount of these input is variable ie the user is free to increase more orders (input) so you should send an array with the value of these inpuT. but I have no idea how to do it. Here I leave my code, I hope you can help me. THANK YOU IN ADVANCE
function getInput(type, placeholder,clase,name){
var nodo = document.createElement("input");
nodo.type = type;
nodo.placeholder = placeholder;
nodo.className = clase;
nodo.name = name;
nodo.required ="";
return nodo;
}
function append(className, nodoToAppend){
var nodo = document.getElementsByClassName(className)[0];
nodo.appendChild(nodoToAppend);
}
function agregaCaja(){
var nodo = getInput("number","el precio","precio","precio");
append("formulario",nodo);
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<form >
<div class="formulario">
<input type="number" placeholder="precio" class="precio">
</div>
<br>
<input type="submit" id="btnregistrar" value="enviar">
<input type="button" value="agregar" onclick="agregaCaja()">
</form>
</body>
<script>
$(document).ready(function(){
$("#btnregistrar").click(function(){
var subTotal = $(".precio");
$.get("/LuanTextilesProyecto/prueba",{"subTotal[]":[subTotal[0].value,subTotal[1].value]}, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</html>
try{
String[] subtotal = request.getParameterValues("subTotal[]");
out.println("<h3>subtotal :"+subtotal[0]+"</h3>");
}
catch(Exception e){
out.println("<h1> el pedido no se pudo registrar</h1>");
}