Send checkbox data to php to update

0

I'm new to this ... any help is welcome.

I have a modal view where I load a table, I have a checkbox for each row, I want to update the fields that are only selected, (get the values of the elements that are selected) and then make an update, now this is my code Thank you very much to the people who can help me.

 
<td><?php echo $verDetalle['itemCode'] ?></td>
<td><?php echo $verDetalle['dscription'] ?></td>
<td><?php echo $verDetalle['causal'] ?></td>
<td><?php echo $verDetalle['mensaje'] ?></td>
    <td>
      <input type="checkbox" value="<?php echo $verDetalle['id_linea'] ?>" id ="ErrorPrecio"/>
    </td>

with the id of the checkbox I'm getting the value with the property .val () but only metes a single value ... the first one

this is what I do with javaascript

$("#prueba").click(function(){

errorPrecio=$('#ErrorPrecio').val();

  cadena= "errorPrecio="+ errorPrecio;

alert(cadena);

      $.ajax({
        type:"POST",
        url:"../php/actualizaPrecio.php",
        data:cadena,
        success:function(r){
          if (r==false) {

          alertify.error("Fallo la transaccion, Intente de nuevo o contacte al Administrador");

          }else{

          alertify.success("Actualizado con exito");


          }
        }


      });


    });
    
asked by Diyer moreno Hernández 11.10.2018 в 02:16
source

1 answer

0

I do not understand what you need to do, but if you want to run an update using AJAX based on the checkbox, you need to check each checkbox, you can do it in the JQuery:

if($('#ErrorPrecio').prop('checked')){
    //realizas tu petición AJAX...
}

Now, if you need it for several checkboxes, the data is stored in a single variable and you send it in an AJAX, obviously you should have a php script that is prepared to receive multiple data and make the corresponding updates. Here you can get more information about the prop () method.

Update

For several checkboxes you will have to do your respective check for each one and add it to your variable that you will send via AJAX:

var data = "?";
if($('#checkbox1').prop('checked')){
    data += "checkbox1="+('#checkbox1').val()+"&";
}
if($('#checkbox2').prop('checked')){
    data += "checkbox2="+('#checkbox1').val()+"&";
}
//....

The next thing would be to run your AJAX and in your php script you should look for each variable that you have sent:

<?php
if(isset($_POST['checkbox1'])){
    //...haces lo que necesitas para el checkbox1
}
//haces lo mismo para los demás
?>
    
answered by 11.10.2018 в 03:17