0

Hi I want to use Multi array of checkbox with php , and i want to get all values in each array checked or not checked . my problem is the array is content only the checked value .

this is my code :-

  if($_POST['send']){
            $co = count($_POST['recomID']);
               for($i=0; $i<= $co -1 ;$i++) {
 $result = mysql_query("UPDATE `recom` SET
 `crit1` = '".$_POST['ch1'][$i] ."',
 `crit2` = '".$_POST['ch2'][$i]."',
 `crit3` = '".$_POST['ch3'][$i]."',
 `crit4` = '".$_POST['ch4'][$i]."', WHERE `id` = '".$_POST['recomID'][$i]."'");
               }
       }


 while($recomObject = mysql_fetch_object($recomResult)){

    echo '   
    <tr>
    <td>'.$recomObject->op.'</td>
    <td align="center"><input type="checkbox" value="1" name="ch1[]" /></td>
    <td align="center"><input type="checkbox" value="1" name="ch2[]" /></td>
    <td align="center"><input type="checkbox" value="1" name="ch3[]" /></td>
    <td align="center"><input type="checkbox" value="1" name="ch4[]" /></td>
    <td><input type="hidden" name="recomID[]" value="'.$recomObject->id.'"/>
    </td>
    </tr>';}
Mustafa
  • 3
  • 4

3 Answers3

2

I have ran into this situation before and I resolved by placing a hidden input before the checkbox with the same name. If the checkbox is checked then that value will override the hidden. This should work for you.

The second input always overrides the first. In this case checkboxes don't POST if unchecked which means the hidden input would POST a value of 0

PHP:

<?php 

if (isset($_POST['ch1'])) {
    echo '<pre>', print_r($_POST['ch1'], true), '</pre>';
    echo '<pre>', print_r($_POST['ch2'], true), '</pre>';
    echo '<pre>', print_r($_POST['ch3'], true), '</pre>';
    echo '<pre>', print_r($_POST['ch4'], true), '</pre>';
}

?>

HTML:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
    <!-- Row 1 Checkboxes -->
    <input type="hidden" value="0" name="ch1[0]" />
    <input type="checkbox" value="1" name="ch1[0]" />
    <input type="hidden" value="0" name="ch2[0]" />
    <input type="checkbox" value="1" name="ch2[0]" />
    <input type="hidden" value="0" name="ch3[0]" />
    <input type="checkbox" value="1" name="ch3[0]" />
    <input type="hidden" value="0" name="ch4[0]" />
    <input type="checkbox" value="1" name="ch4[0]" />

    <br />

    <!-- Row 2 Checkboxes -->
    <input type="hidden" value="0" name="ch1[1]" />
    <input type="checkbox" value="1" name="ch1[1]" />
    <input type="hidden" value="0" name="ch2[1]" />
    <input type="checkbox" value="1" name="ch2[1]" />
    <input type="hidden" value="0" name="ch3[1]" />
    <input type="checkbox" value="1" name="ch3[1]" />
    <input type="hidden" value="0" name="ch4[1]" />
    <input type="checkbox" value="1" name="ch4[1]" />

    <!-- And so forth... -->

    <input type="submit">
</form>

[x] [ ] [x] [ ]
[ ] [x] [ ] [x]  [ SUBMIT ]

Output:

Array
(
    [0] => 1
    [1] => 0
)
Array
(
    [0] => 0
    [1] => 1
)
Array
(
    [0] => 1
    [1] => 0
)
Array
(
    [0] => 0
    [1] => 1
)

Edit

$i = 0;
while($recomObject = mysql_fetch_object($recomResult)){
    echo '   
        <tr>
        <td>'.$recomObject->op.'</td>
        <input type="hidden" value="0" name="ch1['.$i.']" />
        <input type="hidden" value="0" name="ch2['.$i.']" />
        <input type="hidden" value="0" name="ch3['.$i.']" />
        <input type="hidden" value="0" name="ch4['.$i.']" />
        <td align="center"><input type="checkbox" value="1" name="ch1['.$i.']" /></td>
        <td align="center"><input type="checkbox" value="1" name="ch2['.$i.']" /></td>
        <td align="center"><input type="checkbox" value="1" name="ch3['.$i.']" /></td>
        <td align="center"><input type="checkbox" value="1" name="ch4['.$i.']" /></td>
        <td><input type="hidden" name="recomID[]" value="'.$recomObject->id.'"/>
        </td>
        </tr>';
    $i++;
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • wouldn't you want the hidden fields second so you always just check the first item for a 1 or 0? – UnholyRanger Mar 22 '13 at 18:30
  • @unholyRanger Having them second means they would override the checked value. You would always get array values of false or 0 – jnthnjns Mar 22 '13 at 18:31
  • I do this but when I checked all the result is 1,0,1,0 ...etc – Mustafa Mar 22 '13 at 18:34
  • @Asok you will get an array `array([0] => "0")` on unchecked and `array([0] => "1", [1] => "0")` on checked if you do hidden second. If no `[]` was in the names, they would override – UnholyRanger Mar 22 '13 at 18:39
  • @UnholyRanger You are correct, I fixed it, I forgot that when dealing with arrays I had to declare the index. See my edited answer above. – jnthnjns Mar 22 '13 at 18:44
  • @Asok Its ok , but I use it inside while loop it means each input is duplicate in the same row . hidden = ch1[0] but checkbox = ch1[1]. Do you understand me ? – Mustafa Mar 22 '13 at 19:45
  • @Mustafa Yeah, then you should use an auto-incrementing variable just like your `for` loop. I have modified my answer again. See the **edit** section above – jnthnjns Mar 22 '13 at 19:51
0

You need to have the same name for every checkbox, but a different value:

<td align="center"><input type="checkbox" value="0" name="ch[]" /></td>
<td align="center"><input type="checkbox" value="1" name="ch[]" /></td>
<td align="center"><input type="checkbox" value="2" name="ch[]" /></td>
<td align="center"><input type="checkbox" value="3" name="ch[]" /></td>

Now, to get an array where every checked box is 1 and every unchecked box is 0, after submit do...

if (isset($_POST['ch'])) { // assuming form method = post

    $max = 3 // set number of checkboxes -1

    for ($i = 0;$i <= $max;$i++)

    $ch[$i] = intval(in_array($i,$_POST['ch']));

} else $ch = array();

EDIT: to get 0 or 1 for every single checkbox, do...

<td align="center"><input type="checkbox" value="1" name="ch1" /></td>
<td align="center"><input type="checkbox" value="1" name="ch2" /></td>
<td align="center"><input type="checkbox" value="1" name="ch3" /></td>
<td align="center"><input type="checkbox" value="1" name="ch4" /></td>

Then after submit...

if (isset($_POST['ch1'])) $ch1=0; else ch1=1;
...

If you have large numbers of checkboxes, you would iterate trough $_POST with foreach

michi
  • 6,565
  • 4
  • 33
  • 56
0

This method will give you the checked/unchecked status along with 4 arrays

form:

html>
<form method="post">
    <input type="hidden" name="ch1[]" value="0">
    <input type="checkbox" name="ch1[]" value="1">
    <input type="hidden" name="ch2[]" value="0">
    <input type="checkbox" name="ch2[]" value="1">
    <input type="hidden" name="ch3[]" value="0">
    <input type="checkbox" name="ch3[]" value="1">
    <input type="hidden" name="ch4[]" value="0">
    <input type="checkbox" name="ch4[]" value="1">
    <input type="submit">
</form>

The following will give one of 2 arrays (for each chX) when submitted

unchecked:

array([0] => "0")

checked:

array([0] => "1", [1] => "0")

Therefore you will always have a value in the [0] index. Example PHP:

if(isset($_POST['ch1'][0])){ //do check anyway
  echo $_POST['ch1'][0];
}
UnholyRanger
  • 1,977
  • 14
  • 24