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>';}
10
  • Please explain the need for four arrays. Commented Mar 22, 2013 at 18:26
  • possible duplicate Post the checkboxes that are unchecked Commented Mar 22, 2013 at 18:26
  • see the image that added to question . Commented Mar 22, 2013 at 18:32
  • Need more info. Do you need to look at the values by row or column? Then what do you need to do Commented Mar 22, 2013 at 18:36
  • I will add my fetch data code to the question. Commented Mar 22, 2013 at 18:38

3 Answers 3

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++;
}
Sign up to request clarification or add additional context in comments.

7 Comments

wouldn't you want the hidden fields second so you always just check the first item for a 1 or 0?
@unholyRanger Having them second means they would override the checked value. You would always get array values of false or 0
I do this but when I checked all the result is 1,0,1,0 ...etc
@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 You are correct, I fixed it, I forgot that when dealing with arrays I had to declare the index. See my edited answer above.
|
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

2 Comments

@Mustafa: Why you need four arrays? Can you explain in detail what is your goal?
See the image that added to question .
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];
}

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.