Get 2 input with same name

0

I'm trying to get by name 2 inputs that generates by jQuery a select (that's why the counter) then make a REQUEST in ajax with a foreach that traverses input by input to make the save, the problem is that I get the 2 values of the input at the same time:

function guardarDescripcion() {
    var inclusions1 = '';
    var incCount = document.getElementsByName("inclusions1[]").length;
    for(i=0;i<incCount;i++){
        inclusions1 = + inclusions1 document.getElementsByName("inclusions1[]")[i].value; 
    }
    $.ajax({
        url: "ajax_save.php",
        data: "&inclusions1=" + inclusions1, 
        type: "GET",
        timeout: 10000,
        success: function () {     
            alert("Datos guardados exitosamente");
        },
        error: function () {
            alert("Error al guardar el opcional.");
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="inclusions1[]" class="form-control " value="TEST1" style="width: 246px; margin: 4px 0px;">
<input type="text" name="inclusions1[]" class="form-control "  value="TEST2" style="width: 246px; margin: 4px 0px;">

<button type="button" onclick="guardarDescripcion();">Save</button>
    
asked by Nacho Carpintero 26.09.2018 в 14:33
source

1 answer

0

Taking into account your last comment, you can place your values in an array, then send it by GET:

function guardarDescripcion() {
    var inclusions1 = [];
    var incCount = document.getElementsByName("inclusions1[]").length;
    for(i=0;i<incCount;i++){
        inclusions1[i] = document.getElementsByName("inclusions1[]")[i].value; 
    };
    console.log("Array: " + inclusions1); //
    console.log("Para el request GET: " + inclusions1.toString());
    $.ajax({
        url: "ajax_save.php",
        data: "&inclusions1=" + inclusions1.toString(), //a String
        type: "GET",
        timeout: 10000,
        success: function () {     
            alert("Datos guardados exitosamente");
        },
        error: function () {
            alert("Error al guardar el opcional.");
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" name="inclusions1[]" class="form-control " value="TEST1" style="width: 246px; margin: 4px 0px;">
<input type="text" name="inclusions1[]" class="form-control "  value="TEST2" style="width: 246px; margin: 4px 0px;">

<button type="button" onclick="guardarDescripcion();">Save</button>
    
answered by 26.09.2018 / 18:12
source