mardi 19 septembre 2017

Store a checked checkbox from a form in a session

i want to store a checkbox value in a session if a user cheked the box. If so, the box should be checked after reload the page, too. If a user uncheck the box, the session should be destroyed and the box must be unchecked. Default is uncheked.

I tried different codes, but nothing will work. On my example the chekbox is always checked, even if the user cheked it or not.

Thank you so much!

My php:

session_start();

if( !empty($_POST['debug_on']) ) {
    $_SESSION['debug'] = true;
} else {
    $_SESSION['debug'] = false;

}

My form:

<form class="form-inline" name="form" method="post" action="">
<div class="form-group">
<label class="sr-only" for="inlineFormInputGroup">Email</label>
  <div class="input-group mb-2 mr-sm-2 mb-sm-0">
    <div class="input-group-addon">@</div>
    <input type="email" name="email" class="form-control" id="inlineFormInputGroup" placeholder="name@mail.com" value="<?= $email ?>" required >
  </div>
</div>

<div id="debugbox">
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
    <input type="checkbox" class="custom-control-input" name="debug_on" id="debugbox" value="1" <?php if($_SESSION['debug'] = true); echo "checked='checked'"; ?>>
    <span class="custom-control-indicator"></span>
    <span class="custom-control-description">Output debug</span>
  </label>
 </div>
<button type="submit" class="btn btn-outline-success">Submit</button>
<br>
<br>

</form>

Aucun commentaire:

Enregistrer un commentaire