samedi 30 janvier 2016

Keep checkbox checked, after refresh and submit

I'm trying to keep a checkbox checked, but after I refresh the page, if the checkbox is checked the database will get updated with 'yes', but the database automatic updates it to 'no' even if the checkbox is checked with local.store, I hope somebody can help me. This is my code.

    <form action="" method="POST">
     <label for="option1">Waarschuwingsbericht inschakelen voordat het volgende pack wordt geopend als jou pack boven de 200.000 waard is?</label><input id='option1' type="checkbox" name="checkbox" value="yes"><br>
      <input type="submit" value="Opslaan en nog een pack openen">
    </form>
<?
    if(isset($_POST['checkbox'])){
        $sql = "UPDATE users SET puntenchecked = 'yes' WHERE username = '" . $usernamez . "'";
        $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
    }
    else {
        $sql = "UPDATE users SET puntenchecked = 'no' WHERE username = '" . $usernamez . "'";
        $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
    }
?>
<script>
$(function(){
    var test = localStorage.input === 'true'? true: false;
    $('input').prop('checked', test || false);
});

$('input').on('change', function() {
    localStorage.input = $(this).is(':checked');
    console.log($(this).is(':checked'));
});
</script>




Aucun commentaire:

Enregistrer un commentaire