KaoriYui KaoriYui - 1 year ago 25
PHP Question

Passing a value from a checkbox with multiple values using ajax

I am trying to get the value of a checkbox inside a PHP do while loop, passing the selected item value into a process page (get-mod.php) i am using below code but my console returns

[object NodeList]
any suggestions will help.

My PHP do while loop.

$i = 1;

do{

echo '<input type="checkbox" name="checkboxG[]" id="checkboxG'.$i.'" class="css-checkbox" checked="checked" value="'.$row['modelID'].'" /><label for="checkboxG'.$i.'" class="css-label">'.summary($row['model']).'</label><br/>' ;

$i++;

}while($row = mysql_fetch_assoc($c));


My ajax code.

$(document).on('change','.css-checkbox',function(){

var list = document.getElementsByName('checkboxG[]');
console.log("Getting data for "+list);

$.ajax({
type: 'POST',
url:'get-mod.php',
data:'getID='+list,
success:function(html){

$('#result1').html(html);

}
});
});


I am trying to pass the result inside a div with a id of result1, here is my php code (get-mod.php), but for now i just want to return the values of selected checkboxes.

$r = $_POST['getID'];

print_r($r);

Answer Source

You can try this solution.

var data = { 'getID[]' : []};
$("input:checked").each(function() {
  data['getID[]'].push($(this).val());
});

And also

data:data,

Then print $_POST in PHP file you get all selected data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download