-3

How can I keep checkbox checked after submitting a form? I tried using this method:

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

but all of my checkbox will keep checked even if I only select one, here is my code:

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

Thanks!

2 Answers2

1

change :

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

To :

<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>
bharat parmar
  • 302
  • 3
  • 14
1

I came across this now and wanted to add to the answer by bharat parmer. Instead of checking each array element individually, I used the following as my PHP section:

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

That seemed more direct, especially when the array could be large.