vendredi 25 août 2017

PHP keep checkbox checked after submitting POST form

How can I keep checkbox checked after submitting a form? I tried to 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!




Aucun commentaire:

Enregistrer un commentaire