【问题标题】:PHP keep checkbox checked after submitting POST form提交POST表单后PHP保持复选框选中
【发布时间】:2020-07-19 23:32:28
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 请编辑您的帖子并将您的 html 放入 html 或代码块中
  • 对不起,我忘了把它放在括号里,,,,我是新手
  • 尝试将 required="required" 改为 required
  • @BarclickFloresVelasquez 它不会改变任何东西

标签: php checkbox


【解决方案1】:

改变:

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

收件人:

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

【讨论】:

  • 老兄,非常感谢。上帝保佑你和你的家人
  • 欢迎.. 投票并接受答案。快乐编码.. :)
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2015-01-22
    • 2017-05-20
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多