mardi 1 octobre 2019

keep checkbox checked after POST

I have an array of checkbox and after POST the check mark disappears. I've tried different solutions but doesn't seem to work. I'm not sure if it is due to the complicated values of the checkbox. The values have special characters, I'm not sure if it matters. Please see my code below.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
<?php 
        if (isset($_POST["check"]))
              $brand = $_POST["check"];
    else 
      $check="";
 ?>

     <input type="checkbox" name="check[]" value="request LIKE '%&lt;text1&gt;%'" <?php if(isset($_POST["check"][0]))
      { if($_POST["check"][0]=="request LIKE '%&lt;text1&gt;%'"){ echo 'checked="checked"';} } ?>><label>Option 1</label>

     <input type="checkbox" name="check[]" value="request LIKE '%&lt;text2&gt;%'" <?php if(isset($_POST["check"][0]))
      { if($_POST["check"][0]=="request LIKE '%&lt;text2&gt;%'"){ echo 'checked="checked"';} } ?>><label>Option 2</label>

  </form>

This doesn't work as well:

<input type="checkbox" name="check[]" value="request LIKE '%&lt;text1&gt;%'"<?=(in_array("request LIKE '%&lt;text1&gt;%'", $check) ? ' checked="checked"' : '') ?> />



Aucun commentaire:

Enregistrer un commentaire