表单仅在您点击复选框时才有效

时间:2017-06-04 21:14:09

标签: php html mysql

我正在制作表格,我希望在勾选或勾选复选框时能够提交表格。我在登录时没有收到任何其他错误而没有选中除此之外的方框

  

注意:第91行的/home/sites/2a/d/dbcf5d6440/public_html/MySQL/Challenges/Secretdiary.php中的未定义索引:stayLoggedIn

这是我的代码!

<?php

session_start();

 $error="";

if (array_key_exists("logout", $_GET)) {

    unset($_SESSION);
    setcookie("id", "", time() - 60*60);
    $_COOKIE["id"] = "";

} else if(array_key_exists("id", $_SESSION) OR array_key_exists("id", $_COOKIE)) {

    header("Location: loggedinpage.php");
}

    if(array_key_exists("submit", $_POST)) {

        $link = mysqli_connect("XXX", "XXX", "XXX", "XXX");

            if (mysqli_connect_error()) {

                die("Database Connection Error");
            }



        if(!$_POST['email']) {

            $error.= "An Email Address is required<br>";
        }

        if(!$_POST['password']) {

            $error.= "A Password is required<br>";
        }

        if($error != "") {

            $error = "<p>There were error(s) in your form</p>".$error;

        } else {

            if($_POST['signUp'] == '1' ) {

            $query = "SELECT id FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1";
            $result = mysqli_query($link, $query);
            if (mysqli_num_rows($result) > 0) {

                $error = "That email address is taken.";

            } else {

                $query = "INSERT INTO `users` (`email`, `password`) VALUES('".mysqli_real_escape_string($link, $_POST['email'])."', '".mysqli_real_escape_string($link, $_POST['password'])."')";

                if (!mysqli_query($link, $query)) {

                    $error="<p>Could not sign you up, please try again</p>";

                } else {

                    $query = "UPDATE `users` SET password = '".md5(md5(mysqli_insert_id($link)).$_POST['password']). "' WHERE id = ".mysqli_insert_id($link)." LIMIT 1";

                    mysqli_query($link, $query);
                    $_SESSION['id'] = mysqli_insert_id($link);
                    if ($_POST['stayLoggedIn'] == '1') {

                        setcookie("id", mysqli_insert_id($link), time() +  60*60*24*365);
                    }


                } header("Location: loggedinpage.php");


            } 


        } else {

                $query = "SELECT * FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."'";

                $result = mysqli_query($link, $query);
                $row = mysqli_fetch_array($result);
                if (array_key_exists("id", $row)) {

                    $hashedPassword = md5(md5($row['id']).$_POST['password']);
                    if ($hashedPassword == $row['password']) {

                        $_SESSION['id'] = $row['id'];
                        if ($_POST['stayLoggedIn'] == '1') {

                            setcookie("id", $row ($link), time() +  60*60*24*365);
                    }
                    }
                } 

                }


    }

    }



?>




<div id="error"><?php echo $error; ?></div>
<form method="post">
    <input type="email" name="email" placeholder="Your Email Eg. JoBloggs@me.co.uk">
    <input type="password" name="password" placeholder="Password">
    <input type="checkbox" name="stayLoggedIn" value=1>
    <input type="hidden" name="signUp" value="1">
    <input type="submit" name="submit" value="Sign Up!">
</form>

<form method="post">
    <input type="email" name="email" placeholder="Your Email Eg. JoBloggs@me.co.uk">
    <input type="password" name="password" placeholder="Password">
    <input type="checkbox" name="stayLoggedIn" value=1>
    <input type="hidden" name="signUp" value="0">
    <input type="submit" name="submit" value="Log In!">
</form>

3 个答案:

答案 0 :(得分:2)

复选框仅在变为checkecd时作为参数提交。因此,您需要使用collectionView(_:cellForItemAt:)来测试是否设置了复选框,而不是检查值。所以改变

isset()

if ($_POST['stayLoggedIn'] == '1')

答案 1 :(得分:1)

复选框仅发送值WHEN CHECKED。因此,您必须执行class Apple: NICE_APPLES = [] def __init__(self, name, colour): self.name = name self.colour = colour Apple.NICE_APPLES = [Apple('Elstar', 'Green'), Apple('Braeburn', 'Red'), Apple('Pink Lady', 'Pink')] 以查看是否已选择该值,否则该值将不存在于PHP POST值中。

答案 2 :(得分:1)

如果未选中复选框,则复选框的值不会发送。您可以通过isset函数检查kubeadm索引是否存在:

stayLoggedIn