1

I am working on a html form where I have a number of checkboxes, but I want to pass only the unchecked values to another form (get_value.php).

   <form action="get_value.php" method="post">
     <input type='checkbox' name='products[]' value='call1'>col1<br>
     <input type='checkbox' name='products[]' value='call2'>col2<br>
     <input type='checkbox' name='products[]' value='call3'>col3<br>
     <input type='checkbox' name='products[]' value='call4'>col4<br>
     <input type='submit'  value='Submit'>
   </form>

Is there any way to filter $_POST['products'] to get unchecked values only?

GMchris
  • 4,979
  • 4
  • 21
  • 38
ankit kumar
  • 65
  • 1
  • 9

2 Answers2

1

Unchecked checkboxes are not sent at all. If you, for example, check col1 and col2, the following POST data will be sent to get_value.php:

array(
    'products' => array(
        'call1',
        'call2',
    ),
)

If you want to determine which ones weren't checked, do this:

$values = array(
    'call1',
    'call2',
    'call3',
    'call4',
);

$unchecked = array_diff($values, $_POST['products']);

var_dump($unchecked);

Result:

array(2) {
  [2]=>
  string(5) "call3"
  [3]=>
  string(5) "call4"
}
ShiraNai7
  • 6,129
  • 2
  • 23
  • 26
0

You can also get unchecked checkbox values in javascipt.

Try this

$('#q').submit(function(e) {
    $('input[type="checkbox"]').each(function(index) {
        if(!this.checked) {
           alert(this.value);
        }
    });
    e.preventDefault();
});

Here q will be formID.

Shalu Singhal
  • 553
  • 3
  • 8