PHP在提交帖子表单后保持复选框处于选中状态

PHP在提交帖子表单后保持复选框处于选中状态,php,checkbox,Php,Checkbox,提交表单后如何保持复选框处于选中状态? 我试着用这个方法: <input type = "checkbox" name="math[]" value="Addition" <?php if(isset($_POST['math'])) echo "checked='checked'";?>/>Addition<br> 更改: <form id="calc" action="calculator.php" method="POST" > <b

提交表单后如何保持复选框处于选中状态? 我试着用这个方法:

<input type = "checkbox" name="math[]" value="Addition" <?php if(isset($_POST['math'])) echo "checked='checked'";?>/>Addition<br>
更改:

<form id="calc" action="calculator.php" method="POST" >
<b>Enter First No:  <br>    
<input type = "text" name="num1" value="<?php if(isset($_POST['num1'])){echo $num1;} ?>" required="required"/><br>
Enter Second No: <br>
<input type = "text" name="num2" value="<?php if(isset($_POST['num2'])){echo $num2;} ?>" required="required"/><br>
<b>Select Operation: <br>
<input type = "checkbox" name="math[]" value="Addition" <?php if(isset($_POST['math'])) echo "checked='checked'";?>/>Addition<br>
<input type = "checkbox" name="math[]" value="Subtraction" <?php if(isset($_POST['math'])) echo "checked='checked'";?>/>Subtraction<br>
<input type ="submit" value="compute" name="btnsubmit"> <br>

输入第一个编号:

我现在遇到了这个问题,想补充一下巴拉特·帕默的答案。我没有单独检查每个数组元素,而是使用以下内容作为我的PHP部分:

<?php if (!empty($_POST['math']) && in_array("Addition", $_POST['math']))
                echo 'checked="checked"';?>


这似乎更直接,尤其是当数组可能很大时。

请编辑您的帖子,并将html放在html或代码块中。很抱歉,我忘了将其放在括号中,,,,我是这篇可能重复的文章的新手,请尝试更改required=“required”以required@BarclickFloresVelasquez这不会改变任何事情,伙计,非常感谢你。上帝保佑你和你的家人欢迎。。投票并接受答案。快乐编码…)
<form id="calc" action="" method="POST" >
<b>Enter First No:  <br>    
<input type = "text" name="num1" value="<?php if(isset($_POST['num1'])){echo $_POST['num1'];} ?>" required="required"/><br>
Enter Second No: <br>
<input type = "text" name="num2" value="<?php if(isset($_POST['num2'])){echo $_POST['num2'];} ?>" required="required"/><br>
<b>Select Operation: <br>
<input type = "checkbox" name="math[]" value="Addition" 
  <?php if(isset($_POST['math'][0]))
  { 
    if($_POST['math'][0]=="Addition"){ echo 'checked="checked"';}
  } ?> />Addition<br>
<input type = "checkbox" name="math[]" value="Subtraction"
  <?php if(isset($_POST['math'][0]) || isset($_POST['math'][1])) 
  {
    if($_POST['math'][0]=="Subtraction"){ echo 'checked="checked"';}
    if(isset($_POST['math'][1])){
      if($_POST['math'][1]=="Subtraction"){ 
        echo 'checked="checked"';
      }
    }
  } ?> />Subtraction<br>

<input type ="submit" value="compute" name="btnsubmit"> <br>
<?php if (!empty($_POST['math']) && in_array("Addition", $_POST['math']))
                echo 'checked="checked"';?>