How to pass a fix by Jquery ajax to a servlet (java)

0

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>
part of the servlet code test
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>");
          }
    
asked by Frank Campos Vilchez 27.06.2018 в 01:44
source

1 answer

0

It would be advisable to create an array in the javaScript and send it to the servlet. And in the servlet when picking it up, you would have to go through it with a for to obtain the data of the array.

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 = [];
              $( "input[type=number]" ).each(function( index ) {
                 subTotal.push( $( this ).val() );
                 console.log( $( this ).val() );
              });
              $.get("/LuanTextilesProyecto/prueba",{"subTotal[]":subTotal}, function(data, status){
                 alert("Data: " + data + "\nStatus: " + status);
                });
             });
           });
        </script>
</html>

part of the servlet code test

    try{

       String[] subtotal = request.getParameterValues("subTotal[]");

       for(int i = 0; i < subtotal.length; i++)
       {
          out.println("<h3>subtotal :"+subtotal[i]+"</h3>");
       }

    }
    catch(Exception e){
         out.println("<h1> el pedido no se pudo registrar</h1>");
    }
    
answered by 27.06.2018 в 12:44