Make multiple insert with checkbox

0

I have only been able to make insert but of only one, when selecting several people I want to make the insert of the ones that are selected, whether they are 3, 4, 5, etc. it's in MVC

EL html

<?php foreach($todo_personas as $todo): ?>
          <?php 

     //print_r($todo);

?>
<td><input class="checkbox1" type="checkbox" name="checkbox1"id= "<?=$todo->id_persona;?>" value="<?=$todo->id_persona;?>"> </td>

      <?php endforeach ?>

/ * --------------------------------- * /

    $('.checkbox1').on('click', function(e){
        id = $(this).val();
        $('#id').val(id);
});

ajax

$('.boton').click(function(){
        var id = $('#id').val();         
        var persona = $('#persona').val();
        var fecha = $('#fecha').val();
        var ciudad = $('#ciudad').val();

    if(inv !='' && fecha !='' && persona !=''){
        $.ajax({
           url: base_url + "contacto/persona",
            type: "POST",
            dataType: "json",
            data : {persona,persona},
            success: function(json, textStatus, jqXHR){
                console.log(json);

                console.log(json.status);
                if (json.status) {
                    registrar(id,persona,fecha);
                }else{
                    swal('Error!', 'no se encuentra', 'error');
                    console.log(json.status);
                }
                },
                complete: function(textStatus, jqXHR){
            },
            error: function(textStatus, errorThrown, jqXHR){
                console.log(textStatus);
                console.log(errorThrown);
                console.log(jqXHR);

            }

        });
    }else{
        swal('Error!', 'Favor de llenar todos los campos', 'error');
    }
});

Function

    function registrar(id,persona,fecha)
 {

            $.ajax({
               url: base_url + "contacto/registrar",
                type: "POST",
                dataType: "json",
                data : {id : id, persona : persona, fecha : fecha},
                success: function(json){
                swal('Bien!', 'Se a enviado ', 'success').then(function(){
                    });
                },
                complete: function(textStatus, jqXHR) {
                },
                error: function(textStatus, errorThrown, jqXHR){
                    console.log(textStatus);
                    console.log(errorThrown);
                    console.log(jqXHR);
                }
                });

 }
    
asked by Juan Jose 02.10.2018 в 17:59
source

2 answers

0

var array__ = [];
  $.each($(".checkbox1"),function(index,val){
    console.log($(val).prop("checked"));
    if($(val).prop("checked")==true){
      array__.push($(val).val());
    }
});

console.log(array__);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="checkbox1" checked type="checkbox" id= "1" value="valor1">

<input class="checkbox1" checked type="checkbox" id= "2" value="valor2">

<input class="checkbox1" type="checkbox" id= "3" value="valor3">

<input class="checkbox1" type="checkbox" id= "4" value="valor4">

Then in the ajax you must send the array as a parameter in the data:

data: {person: person, selected: array __}

This data arrives as an array to PHP where you can play with it.

    
answered by 02.10.2018 в 19:19
0

Grab part of your code and emulate the table with fictitious data, and in each row place a checkbox, then at the end place a button to get the checkbox that are checked or :checked ; if you notice this button has a click event that is the part where you have to guide which I go through all the checbox with the class .checkbox1 that are checked, if there is one I get all the values of their row and I store them in an array which has the name arrayFila , it should be noted that every time you fill the array is a value or an array type json , so you will get to PHP well. In the php you will go through it with a foreach or a for as you can be better and each index as its value is a json you should treat it as such I mean to put a json_decode() to get their values.

P.D: Place a console.log(arrayFila) so that you can see how the array is filling up, and in the ajax set how I am doing in data .

$("#guardar").on("click", () => {

  var arrayFila = [];
  
  $(".checkbox1:checked").each((indice, elemento) => {
    let fila = $(elemento).parents("tr");
    let id = $(elemento).val();
    let persona = fila.find(".persona").val();
    let fecha = fila.find(".fecha").val();
    let ciudad = fila.find(".ciudad").val();
    
    arrayFila.push({id:id, persona:persona, fecha:fecha, ciudad:ciudad});
  });
  
  console.log(arrayFila);
  let base_url  = "";
  $.ajax({
    url: base_url + "contacto/registrar",
    type: "POST",
    dataType: "json",
    data : {personas : arrayFila},
    success: function(json){},
    complete: function(textStatus, jqXHR) {},
    error: function(textStatus, errorThrown, jqXHR){}
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="text" class="persona" value="David Molina">
    </td>
    <td>
      <input type="text" class="fecha" value="02/10/2018">
    </td>
    <td>
      <input type="text" class="ciudad" value="Caracas">
    </td>
    <td>
      <input class="checkbox1" type="checkbox" name="checkbox1" id="1" value="1">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" class="persona" value="Leonardo Ruíz">
    </td>
    <td>
      <input type="text" class="fecha" value="01/10/2018">
    </td>
    <td>
      <input type="text" class="ciudad" value="Valencia">
    </td>
    <td>
      <input class="checkbox1" type="checkbox" name="checkbox1" id="1" value="2">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" class="persona" value="Freddy Manuel">
    </td>
    <td>
      <input type="text" class="fecha" value="03/10/2018">
    </td>
    <td>
      <input type="text" class="ciudad" value="Bejuma">
    </td>
    <td>
      <input class="checkbox1" type="checkbox" name="checkbox1" id="1" value="3">
    </td>
  </tr>
  <tr>
    <td colspan="4">
      <button id="guardar">Guardar</button>
    </td>
  </tr>
</table>
    
answered by 02.10.2018 в 19:24