1

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);
3
  • You should use $(this) to get the checkbox that they changed. And don't you need to distinguish whether the box is checked or unchecked? Commented May 23, 2017 at 5:19
  • Your loop is wrong. It should be a while loop, not a do-while loop, because you need to fetch the row before you put the value into the checkbox. Commented May 23, 2017 at 5:20
  • 1
    If you wrap your checkbox inside one form with specific id , you can get by $("form-id").serialize() .That's all Commented May 23, 2017 at 5:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.