如果表单有1个或多个错误,则保留表单复选框值


form check box value retain if form having 1 or more error

$facility=$_POST['interests'];

<input type="checkbox" name="interests[]" id="" value="wifi" value="<?php echo in_array('wifi', $facility)??>checked='checked'<?php:;?>" /><label class="check_label">Wifi</label>
<input type="checkbox" name="interests[]" id="" value="spa"  value="<?php echo in_array('spa', $facility)?>checked='checked'<?php:;?>"/><label class="check_label">Spa</label>
<input type="checkbox" name="interests[]" id="" value="pet allowed" value="<?php echo in_array('pet allowed', $facility)?>checked='checked'<?php:;?>"/><label class="check_label">Pet Allowed</label>

第二种方法:-

<input type="checkbox" name="interests[]" id="" value="wifi" value="<?php  in_array('wifi', $facility){?>checked='checked'<?php}?>" /><label class="check_label">Wifi</label>
<input type="checkbox" name="interests[]" id="" value="spa"  value="<?php  in_array('spa', $facility){?>checked='checked'<?php}?>"/><label class="check_label">Spa</label>
<input type="checkbox" name="interests[]" id="" value="pet allowed" value="<?php  in_array('pet allowed', $facility){?>checked='checked'<?php}?>"/><label class="check_label">Pet Allowed</label>

我应用这两种方法是为了在用户犯了任何错误的情况下保留表单中的post值。如果他犯了错误,则在提交后保留复选框,但不起作用。

最终我修复了这个错误

 <input type="checkbox" name="interests[]" id="" value="wifi" <?php if(in_array('wifi', $facility)==TRUE){echo "value=wifi checked='checked'";}?> /><label class="check_label">Wifi</label>
                    <input type="checkbox" name="interests[]" id="" value="spa"  <?php if(in_array('wifi', $facility)==TRUE){echo "value=spa checked='checked'";}?>/><label class="check_label">Spa</label>
                    <input type="checkbox" name="interests[]" id="" value="pet allowed" <?php if(in_array('wifi', $facility)==TRUE){echo "value=pet allowed checked='checked'";}?>/><label class="check_label">Pet Allowed</label>

以前的脚本,我使用的逻辑错误:-

<?php  
  in_array('wifi', $facility){
?> checked='checked'
<?php}?>" //there is no if else exist how it check condition is true or false

现在我更改脚本并使用条件:-

<?php 
      if(in_array('wifi', $facility)==TRUE){
         echo "value=wifi checked='checked'";
      }
?>