使用PHP提交表单后,如何保持复选框处于选中状态


How can I keep the checkbox checked after I submit a form using PHP?

我试图从post数组中获取一个值,然后将其存储在一个变量中,然后使用该值来保持选中复选框。以下是我正在尝试的:

我在前臂上方有一行:

    <input type='checkbox' name='$plyrMtchResult 'value=''>$plyrMtchResult</input>  //This line works fine
 //then when I submit the form the foreach loop runs here.
  foreach($_POST as $value => $key)
        {
                $post_Value= str_replace("_", " ", $value);
                "<input type='checkbox' name='$post_Value' value='post_valuebValue' checked='checked'>$post_valuebValue</input>";
        }

我的问题似乎和我在这里发现的相似,但我的问题不起作用。

提交表单后如何保持复选框处于选中状态。

<input type='checkbox' name='$post_Value'     value='post_valuebValue'    checked='checked'>$post_valuebValue</input>

我会做这样的事情,请原谅在我的手机上做这件事时出现的任何语法错误

<input type='checkbox' name='$post_Value' value='post_valuebValue' <?pho if ($postvalue = checked){echo 'checked='checked'}?>'>$post_valuebValue</input>";

将选中的html回显到字段中以保持其选中

您的代码看起来很混乱,但如果我理解正确,您需要类似的东西

<input type='checkbox' name='$plyrMtchResult 'value='' <?php echo (isset($_POST['$plyMtchResult'])?'checked':''); ?> >$plyrMtchResult</input>

如果您只想为已经存在的复选框元素设置checked值,则不需要运行foreach循环。