Mustafa
Mustafa

Reputation: 3

Multi array of checkbox with php

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>';}

Upvotes: 0

Views: 1735

Answers (3)

jnthnjns
jnthnjns

Reputation: 8925

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++;
}

Upvotes: 2

UnholyRanger
UnholyRanger

Reputation: 1971

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];
}

Upvotes: 0

michi
michi

Reputation: 6625

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

Upvotes: 0

Related Questions