PHP问题与复选框发布

时间:2015-08-13 14:15:15

标签: php

这可能是一个非常愚蠢的问题,但我无法找出问题所在。我有这个表单,我用它作为一种'设置'的东西与复选框的设置。但出于某种原因(我不确定数据是否未被发布或者我的函数是否存在问题)$_SESSION变量从未获得任何数据。

表格代码:

<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函数(提交表单):

$("#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'];
?>

摘要:变量$_SESSION['startupalert']没有填充任何数据。

编辑:忘记提及init.php中只有session_start();

这是使用变量$_SESSION['startupalert']的地方

<?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 个答案:

答案 0 :(得分:1)

只是在评论的解决方案中总结一下:

你的jQuery需要阻止表单提交两次。所以,我推荐类似的东西:

$("#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
    )};
});