Array from JS to php codeigniter

0

I am working on a personal project and I have a question about the passage of data to php.

It turns out that in js I keep changing the colors of the div (the colors are initially loaded through a query to a bd) in an array, with the code and the color (they can be 2, these represent them with 0) or 1).

What I need is that by pressing a button, the states in the bd are updated. The problem is that I can not send the array of js to php. I tried with the following function:

$.ajax({ url: 'cursos/guardarmalla',
 data: {'actualizar' : actualizar},
 type: 'post',
 dataType:'json',
 success: function(output) {
              alert(output);
 }
 });

but I do not know how to put the button together and send the array.

function to send the data:

function enviararray() {

$.ajax({ url: 'cursos/guardarmalla',
 data: {actualizar},
 type: 'post',
 dataType:'json',
 success: function(output) {
              alert(output);
          }
}); 
}




var actualizar=[];
function myButton_onclick(x,id) {
if(x.style.backgroundColor=='rgb(255, 114, 144)')
{
    x.style.backgroundColor='rgb(90,237,247)';
     actualizar[id]="1";

}else{
    x.style.backgroundColor='rgb(255, 114, 144)';
     actualizar[id]="0";

}
console.log(actualizar)
return false;
}

Those are the functions of js that I use to modify the update array.

              

This is the html of the button

<?php echo form_open("/cursos/guardarmalla");?>
   <div align="center"><?php echo form_submit('','Guardar Malla',"class=\"btn btn-success btn-lg\" onclick=\"enviararray()\"");?></div>
<?php echo form_close()?>
    
asked by franco.pina 23.01.2018 в 20:29
source

1 answer

0

If what you want is that when the change is made to the myButton_onclick function, the request would be sent to you at the same time:

//esta es la función que enviara el arreglo con los nuevos datos
function sendActualizar(datos){

  $.ajax({ url: 'cursos/guardarmalla',
      data: {actualizar : datos},
      type: 'post',
      dataType:'json'
  }).done(function(output){
      alert(output)
  }); 
}
//la declaración de tu función myButton_onclick agregándole el envió de los datos
var actualizar=[];
function myButton_onclick(x,id) {
if(x.style.backgroundColor=='rgb(255, 114, 144)')
{
    x.style.backgroundColor='rgb(90,237,247)';
     actualizar[id]="1";
    sendActualizar(actualizar) // manteniendo tu código llamarías aquí a la función que envía el actualizar

}else{
    x.style.backgroundColor='rgb(255, 114, 144)';
     actualizar[id]="0";
     sendActualizar(actualizar) // manteniendo tu código llamarías aquí a la función que envía el actualizar
}
console.log(actualizar)
return false;
}
    
answered by 23.01.2018 в 21:12