2017-08-26 166 views
-3

如何在提交表單後檢查複選框? 我嘗試使用這種方法:提交POST表單後PHP會保留複選框

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

謝謝!

+0

請編輯您的帖子,並把您的HTML html或代碼塊 –

+0

我很抱歉,我忘了把它放在支架上, ,,, Im新到這 –

+0

可能重複[獲取$ \ _ POST來自多個複選框](https://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) –

回答

0

變化:

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

我已經試過,它不工作 –

+0

現在看我編輯的代碼上面 –

+0

這可能工作..榮譽 –