Ajax taking only the first id

Asked

Viewed 64 times

1

Hello, I’m updating a list with arrays and php, passing two presence parameters(ckeckbox) that returns true or false and id in Hidden, but id does not proceed in the loop getting the first id in the two values:

array(2) {
  ["id"]=>
  array(2) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "2"
  }
  ["presenca"]=>
  array(2) {
    [0]=>
    string(5) "false"
    [1]=>
    string(4) "true"
  }

	$(document).ready(function(){
		 $('#btn_update').click(function(){  
		 
		   var id = [];
		   var presenca = [];
		
		   $(':checkbox').each(function(i){
		   
		    presenca[i] = $('#permissao').val(); 
		    id[i] = $('#id').val();
			    if(presenca[i] == this.checked){
			    	this.checked = true;
			    	presenca[i] = this.checked;
			    	
			    }else{
			    	this.checked = false;
			    	presenca[i] =this.checked;
			    	
			    }
		   });
		   
		  
		    $.ajax({
		     url:'<?php echo WEBROOT; ?>/sistema/modulos/processa-permissao.php',
		     method:'POST',
		     data:{id:id, presenca:presenca},
		     success:function()
		     {
		      alert('update ok'); 
		     }
		     
		    });
		   
		 });

The intention is if the user check or not ajax inserts the update in looping.

What am I missing?

  • tries to pass the variables like 'id[]:id', 'presenca[]:presenca' . and puts the console error

  • date line did not continue reading, date:{id:id, presenca:presenca},

1 answer

1

[Solved] I put an attribute in the check box and used attr(); Only doubt remains, type='Hidden' cause single effect on gift?

$(document).ready(function(){
		 $('#btn_update').click(function(){  
		 
		   var id = [];
		   var presenca = [];
		
		   $(':checkbox').each(function(i){
		   
		    presenca[i] = $('#permissao').val(); 
		    //id[i] = $('#id').val(); Aqui está o erro
                      id[i] = $(this).attr("idpega");
			    if(presenca[i] == this.checked){
			    	this.checked = true;
			    	presenca[i] = this.checked;
			    	
			    }else{
			    	this.checked = false;
			    	presenca[i] =this.checked;
			    	
			    }
		   });
		   
		  
		    $.ajax({
		     url:'<?php echo WEBROOT; ?>/sistema/modulos/processa-permissao.php',
		     method:'POST',
		     data:{id:id, presenca:presenca},
		     success:function()
		     {
		      alert('update ok'); 
		     }
		     
		    });
		   
		 });
<input type="checkbox"  id="permissao" '.$permission.' value="'.$relaciona.'" idpega="'.$user_id.'" >
<!-- Criei um atributo para o mesmo checkbox colocando o id -->

Browser other questions tagged

You are not signed in. Login or sign up in order to post.