dimanche 13 mars 2016

isset($_POST['remember']) checkbox issue in PHP

I need to test if a checkbox is checked. I've searched for the solution and found it, but it still don't work for me.

Here's my form :

<form method="post" class="form-horizontal" role="form">
    <div class="input-group">
        <div class="checkbox">
            <label>
                <input id="remember" type="checkbox" name="remember" value="1"> Remember me
            </label>
        </div>
    </div>

    <div style="margin-top:10px" class="form-group">
        <div class="col-sm-12 controls">
            <button type="submit" class="btn btn-success" name="loginform">Login</button>
        </div>
    </div>
</form>

and my PHP code :

if (isset($_POST["loginform"])) {
    if(isset($_POST['remember'])) {
        $month = time() + 2592000;
        debug_to_console($month); // function equivalent to console.log in JS
        setcookie('remember_me', $_POST['username'], $month);
    }
}

EDIT , debug_to_console() function :

function debug_to_console( $data ) {
    if ( is_array( $data ) )
        $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
    else
        $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
    echo $output;
}

EDIT 2 (Whole PHP code) :

<?php
    session_start();

    if(isset($_COOKIE['remember_me']))
        $_SESSION['user'] = $_COOKIE['remember_me'];

    $success = true;

    if (isset($_POST["loginform"])) {
        if(isset($_POST['remember'])) {
            $month = time() + 2592000;
            echo "<h1>Month: $month</h1>";
            setcookie('remember_me', $_POST['username'], $month);
        }

        $password = $_POST['password'];
        $email = $_POST['email'];

        // Form Validation
        if (!$email || !$password || !filter_var($email, FILTER_VALIDATE_EMAIL))
            $success = false;
        else
        {
            Db_connect($connection);

            $email = stripslashes($email);
            $password = stripslashes($password);
            $email = mysqli_real_escape_string($connection, $email);
            $password = mysqli_real_escape_string($connection, $password);

            $query = mysqli_query($connection, "select * from users where email='$email'");
            $rows = mysqli_num_rows($query);

            if ($rows == 1) {

                $row = mysqli_fetch_array($query);
                $hashpass = $row['password'];

                if ( hash_equals($hashpass, crypt($password, $hashpass)) ) {
                    $_SESSION['user'] = $row['username'];
                    header("location: login.php");
                }
                else
                    $success = false;
            } else
                $success = false;

            mysqli_close($connection);
        }
    }

    function Db_connect(&$connection){
        $connection = mysqli_connect("localhost","root","", "project_web");
        if (mysqli_connect_errno())
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    function debug_to_console( $data ) {
        if ( is_array( $data ) )
            $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
        else
            $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";
        echo $output;
    }
?>

When i submit my form with the checkbox checked, and i look at the console, it is empty, while it is supposed to display the value of $month. Which means $_POST['remember'] is not set while it is supposed to be set due to the form submit.

Can anyone help please ?

Thank's in advance.




Aucun commentaire:

Enregistrer un commentaire