PHP中的isset($ _ POST ['remember'])复选框问题

时间:2016-03-13 11:12:04

标签: php forms post checkbox isset

我需要测试是否选中了复选框。我已经搜索了解决方案并找到了它,但它仍然不适用于我。

这是我的表格:

<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>

和我的PHP代码:

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 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;
}

编辑2(整个PHP代码):

<?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;
    }
?>

当我选中复选框提交表单时,我查看控制台,它是空的,而它应该显示$ month的值。这意味着$ _POST ['remember']未设置,因为表单提交应该设置。

有人可以帮忙吗?

提前感谢。

2 个答案:

答案 0 :(得分:1)

$ _ POST ['remember']会是真还是假....删除值= 1 ...你会得到价值......真或假......根据那个来验证......

答案 1 :(得分:1)

  

制作一个像

这样的动作属性
    <form method="post" action="" class="form-horizontal" role="form">
  

php代码

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