dpw5865 2016-05-30 13:38
浏览 69
已采纳

保留PHP中的复选框中的检查值(使用的关联数组)

I am trying to retain the entered values of the checkbox whenever the form gets submitted.

  • 写回答

1条回答 默认 最新

  • doupo2241 2016-05-30 13:51
    关注

    You need to use array in checkbox as you are selecting multiple.

    1 Use name="filling[]" in input checkbox.

    2 Use $filling in foreach loop to fill data.

    PHP code

    <?php
    
    //echo '<pre>'.print_r($_POST,true).'</pre>';
    
    $display_result = false;
    
    $form_data = [
        'filling' => [
            'value' => [],
            'error' => false,
            'err_msg' => '',
        ],
    ];
    
    if ( isset( $_POST['submit'] ) ) {
        $filling = isset($_POST['filling'] ) ? $_POST['filling'] : '' ;
        if( empty($filling) || count($filling) < 2 ) {
            $form_data['filling']['error'] = true;
            $form_data['filling']['err_msg'] = "Please select atleast 2 fllings";
        }
    
        else {
            foreach ($filling as $value) {
                array_push($form_data['filling']['value'], $value);
            }
                //$form_data['filling']['value'] = $filling;
                $form_data['filling']['error'] = false;
        }
    }
    ?>
    

    HTML Code

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Cake ordering Form</title>
    
            <style>
                .reqd {
                    color: red;
                }
    
            </style>
        </head>
        <body>
    
            <form action="" method="post" id="cakeForm">
    
                <fieldset>
                    <p>
                        <label>Fillings: <span class="reqd"><?php print 
                        $form_data['filling']['error'] ? $form_data['filling']['err_msg'] : ''; ?></span></label><br />
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="lemon" <?php print in_array('lemon', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Lemon <br /></label>
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="custard" <?php print in_array('custard', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Custard <br /></label>
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="fudge" <?php print in_array('fudge', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Fudge <br /></label>
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="mocha" <?php print in_array('mocha', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Mocha <br /></label>
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="raspberry" <?php print in_array('raspberry', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Raspberry <br /></label>
                        <label class="checkFill"><input type="checkbox" name="filling[]" value="pineapple" <?php print in_array('pineapple', $form_data['filling']['value'] ) ? 'checked = "checked" ' : ''; ?>/>Pineapple <br /></label>
                    </p>
                </fieldset>
                <p>
                    <input type="submit" value="submit" name="submit" />
                </p>
            </form>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突