How to send values of ckeckboxes not selected and selected to a single array (and try to hide) php?

0

Hello again with my form return this time I have problems with a checkbox by the way and try the hidden button that I'm not so lazy search in Google and it worked but for a single input but I realized that by sending several inputs I I marked an error, since I generate inputs dynamically with jquery, I would appreciate your help to mark the input with a value for example "No" by not selecting the ckeckbox. Either on the client side or on the server side. By the way, it's an array called tagging that I want to insert into the database.

<script>

$(document).ready(function() {
    var max_fields      = 10;
    var wrapper         = $(".container1");
    var add_button      = $(".add_form_field");


    var x = 1;
    $(add_button).click(function(e){
        e.preventDefault();
        if(x < max_fields){
            x++;
                var selectproductos = "<?php $sql = "Select producto from productos"; $query = $db->prepare($sql);
    $query->execute();

       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['producto'].'</option>';
       }
     ?>";

            var selectunidades = "<?php $sql = "Select unidad from unidades";

    $query = $db->prepare($sql);
    $query->execute();
       while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo '<option>'.$row['unidad'].'</option>';
       }
     ?> ";

          $(wrapper).children('table').append('<tr>   <td> <select required name="productos[]"><option value="">Selecciona un Producto</option>'+selectproductos+'</select><td><select required name ="unidad[]"><option value="">Selecciona una unidad</option>'+selectunidades+'</select></td><td><input type="text" class="inputancho" name="cantidad[]" placeholder="cantidad" required="required"/></td><td><input type="date"  name="fecha_de_embarque[]" required="required"/></td>  <td> <textarea  rows="2" cols="30" name="notas[]" id="notas" maxlength="255"></textarea> </td> <td><input type="checkbox" name="etiquetado[]" value="Si">   </td><td><a href="#" class="delete">Eliminar</a></<td></tr>'); //add input box

        }
  else
  {
  alert('You Reached the limits')
  }
    });

   $(wrapper).on("click",".delete", function(e){
    e.preventDefault(); $(this).parent('td').parent('tr').remove(); x--;
})
});

</script>


<input type="hidden" name="etiquetado[]" value="No" />

<input type="submit" name="Submit" onclick="GetTextValue()" id="submit-pedidos" value="Enviar" class="bt"/>


    </form>

    
asked by Daniel Treviño 08.09.2017 в 19:25
source

1 answer

0

Here I leave you two possibilities.

In one it stores the values of each input, in the other it stores the values and the state (checked or unchecked).

$("#btnEnviar").click(function() {
  var arrTodo = new Array();
  var arrValores = $('input[name="cbxTest"]').map(function() {
    arrTodo.push(this.value, this.checked);
    return this.value;

  }).get();

  //Sólo Valores
  console.log("Sólo valores: " + arrValores);

  //Valores y estado
  console.log("Valores y estado: " + arrTodo);


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<legend>Elija opción</legend>
<input type="checkbox" name="cbxTest" value="1" />Opción 1<br />
<input type="checkbox" name="cbxTest" value="2" />Opción 2<br />
<input type="checkbox" name="cbxTest" value="3" />Opción 3<br />
<input type="checkbox" name="cbxTest" value="4" />Opción 4<br />
<hr />
<button id="btnEnviar">Enviar</button>
    
answered by 08.09.2017 в 20:33