简体   繁体   English

刷新并提交后,选中复选框

[英]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. 我正在尝试保持选中复选框,但在刷新页面后,如果选中该复选框,数据库将更新为“是”,但即使使用本地选中复选框,数据库也会自动将其更新为“否” .store,我希望有人可以帮助我。 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>

试试这个 :

<input id='option1' type="checkbox" name="checkbox" value="yes" <?php echo (isset($_POST['checkbox']))? "checked='checked'": "";?> >

Replace below JS logic: 替换下面的JS逻辑:

<script>
$(function(){
    var test = localStorage.input === 'true'? true: false;
    $('input').prop('checked', test);
});

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

Check for more here . 在这里查看更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM