2

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;
        }
    ?>
19
  • call session_start() everytime before you use the $_SESSION variable Commented Aug 13, 2015 at 14:18
  • you forgot to call session_start() Commented Aug 13, 2015 at 14:18
  • @MarcB I forgot to mention that init.php starts the session. Commented Aug 13, 2015 at 14:19
  • You will never reach this statement; echo $_POST['setting1']; due to your header redirect. Commented Aug 13, 2015 at 14:19
  • @vonUbisch That was just for an attempt at debugging which I forgot to remove. Commented Aug 13, 2015 at 14:20

1 Answer 1

1

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
    )};
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.