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

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

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>