duanniubeng2265
2015-08-13 14:15 阅读 11
已采纳

PHP问题与复选框发布

This is probobly a really stupid issue, but I cannot find out what is the issue. I have this form which I am using as a kind of 'settings' thing with checkbox's for the settings. But for some reason (I am unsure whether data is not getting posted or if its an issue with my function) the $_SESSION variable never gets any data in it.

Form code:

<form action="savesettings.php" method="POST" id="CSform">
    <div class="modal-body">
        <P class="noticeme">Please note that all of your settings are stored via cookies</P>
        <div class="checkbox">
            <label><input type="checkbox" name="setting1" value="unchecked">Disable page visit alert on page load</label>
        </div>  
        <br><br><br><br><br><br>
        <div class="text-centered">
            <button class="btn btn-sm btn-success onclickspin" id="MSSave">Submit</button>
        </div>
    </div>
</form>

Jquery function (This submits the form):

$("#MSSave").click(function(){
    $("#MSSave").html('<i class="fa fa-refresh fa-spin"></i>');
    $('#MSSave').prop('disabled', true);
    $("#CSform").submit()
});

savesettings.php:

<?php
    include_once './includes/init.php';

    if (isset($_POST['setting1'])) {
        $_SESSION['startupalert'] = 'off';
    } else {
        $_SESSION['startupalert'] = 'on';
    }
    header('Location: index.php');
    echo $_POST['setting1'];
?>

Summary: The variable $_SESSION['startupalert'] does not get filled with any data.

EDIT: Forgot to mention that init.php only has session_start(); in it.

This is where the variable $_SESSION['startupalert'] is used

<?php
        if ($_SESSION['startupalert'] === 'off') {
            $_SESSION['visits'] = $_SESSION['visits'] + 1;
            } elseif (empty($_SESSION['firstvisit'])) {
                $_SESSION['firstvisit'] = 'false';
                $_SESSION['visits'] = 2; 
                ?> 
                <script> swal("Welcome", "This is your first time on the site", "info"); </script>
                <?php
            } else { 
                ?>
                <script> swal("Welcome back", "This is your <?php echo suffix($_SESSION['visits']) ?> visit", "info"); </script>
                <?php
                $_SESSION['visits'] = $_SESSION['visits'] + 1;
        }
    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doucan1996 doucan1996 2015-08-13 14:54

    Just to summarize in the solution from the comments:

    Your jQuery needs to stop the form from submitting twice. So, I recommend something similar to:

    $("#MSSave").click(function(){
        $("#MSSave").html('<i class="fa fa-refresh fa-spin"></i>');
        $('#MSSave').prop('disabled', true);
        $("#CSform").submit(function(e){
             e.preventDefault;  // this is new to your code
        )};
    });
    
    点赞 评论 复制链接分享

相关推荐