duanlie2709 2013-03-22 18:21
浏览 63
已采纳

多列的复选框与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>';}
  • 写回答

3条回答 默认 最新

  • douangoo48863 2013-03-22 18:28
    关注

    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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!