【问题标题】:Retain checklisted values in checkbox in PHP (used associative array)在 PHP 中的复选框中保留清单值(使用关联数组)
【发布时间】:2016-05-30 13:38:11
【问题描述】:

每当提交表单时,我都会尝试保留复选框的输入值。

【问题讨论】:

    标签: php arrays forms server


    【解决方案1】:

    选择多个时,需要在复选框中使用数组。

    1 在输入复选框中使用name="filling[]"

    2 在 foreach 循环中使用$filling 填充数据。

    PHP 代码

    <?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 代码

    <!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>
    

    【讨论】:

    • 使用此代码它将为您工作。我已经尝试过我的系统,它对我来说工作正常。 @Abhijit Borkakoty
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多