2017-08-26 2 views
-3

Comment puis-je conserver la case à cocher après avoir envoyé un formulaire? Je essayé d'utiliser cette méthode:PHP garder la case à cocher après avoir envoyé le formulaire POST

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

mais toute ma case à cocher garderez vérifié même si je ne sélectionne un, voici mon 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> 

Merci!

+0

S'il vous plaît modifier votre message et mettre votre code html dans le bloc de code HTML ou –

+0

Je suis désolé, j'ai oublié de mettre dans le support, ,,, Im nouveau à ceci –

+0

Possible dupliquer de [Obtenir $ \ _ POST de plusieurs cases à cocher] (https://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) –

Répondre

0

changement:

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

Pour:

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

J'ai déjà essayé, ça ne marche pas –

+0

maintenant voir mon code édité ci-dessus –

+0

Cela pourrait fonctionner .. Kudos –