无法以注册形式显示消息

时间:2017-01-14 13:55:34

标签: php

我对PHP很新,我试图建立一个登录系统。我试图输出错误或成功消息,以显示在与我的表单相同的页面中。我试过回声'用于测试并且它可以工作,但它在新选项卡中显示消息。所以我尝试将消息转换为一个变量,因此在每种情况下,变量都会使用新消息进行更新。然后在register.php页面上,我输入一个PHP代码,我将检查消息是否为空,然后回显变量。问题是我被重定向到register.php页面,只显示没有任何内容。这是我的两个文件的代码,我应该提到注册过程确实有效,如果没有重复项,它会检查电子邮件和用户名,但它不会输出任何消息。

register.php:

<?php
    include 'header.php';
    include 'nav.php';
    include 'handlers/registerhandler.php';
    require 'handlers/dbhandler.php';
?>

    <section>
        <div class="wrapper">
            <p>If you already have an account, click <a href="index.php">here</a> to login.</p>
        </div>

        <div class="wrapper">
            <?php
                if (!empty($msg)) {
                    echo $msg;
                }
            ?>
            <form action="register.php" method="POST">
                <ul>
                    <li><input type="text" name="firstname" placeholder="Enter your first name ..." required></li>
                    <li><input type="text" name="lastname" placeholder="Enter your last name ..." required></li>
                    <li><input type="email" name="email" placeholder="Enter your email adress ..." required></li>
                    <li><input type="text" name="username" placeholder="Enter your username ..." required></li>
                    <li><input type="password" name="password" placeholder="Enter your password ..." required></li>
                    <li><input type="password" name="confirmpassword" placeholder="Confirm your password ..." required></li>
                    <li><button type="submit" name="submit" value=1>Register</button></li>
                </ul>
            </form>
        </div>
    </section>

<?php
    include 'footer.php';
?>

和registerhandler.php:

<?php

session_start();
require 'dbhandler.php';

if (isset($_POST['submit'])) {

    $msg = '';

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $confirmpassword = $_POST['confirmpassword'];

    $usernameExcistCheck = "SELECT * FROM user WHERE username='$username'";
    $usernameCheckResult = mysqli_query($connect, $usernameExcistCheck);
    $emailExcistCheck = "SELECT * FROM user WHERE email='$email'";
    $emailCheckResult = mysqli_query($connect, $emailExcistCheck);

    while ($usernameRow = mysqli_fetch_assoc($usernameCheckResult)) {
        $db_username = $usernameRow["username"];
    }
    while ($emailRow = mysqli_fetch_assoc($emailCheckResult)) {
        $db_email = $emailRow["email"];
    }


    if ($db_username == $username) {
        $msg = "Username already excists.";
    } else {
        if ($db_email == $email) {
            $msg = "E-mail is already registered";
        } else {
            if($password != $confirmpassword) {
            die("Passwords do not match! Please try again.");       
            } else {
                $confirmcode = rand();
                $sql = "INSERT INTO user (firstname, lastname, email, username, password, confirmed, confirmcode)
                VALUES ('$firstname', '$lastname', '$email', '$username', '$password', '0', '$confirmcode')";
                $result = mysqli_query($connect, $sql);

                if ($result) {
                        $to = $email;
                        $subject = "Activate your DuChambre.nl account!";
                        $message = "<html>
                        <head>
                        <title>Activate your DuChambre.nl account</title>
                        </head>
                        <body>
                        <p>Dear $firstname $lastname,<br><br>Your account needs to be activated in order to use it. Please click <a href='http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email'>here</a> to activate your account. You could also copy and paste the link below.<br><br>http://www.duchambre.nl/duchambre_loginsystem/verifyemail.php?code=$confirmcode&email=$email<br><br>Please do not reply to this email. Enjoy your stay!<br><br>Cheers,<br>Bas
                        </p>
                        </body>
                        </html>";

                        $headers = "MIME-Version: 1.0" . "\r\n";
                        $headers .= "Content-type: text/html; charset=iso-8859-1" ."\r\n";
                        $headers .= 'From: <activation@duchambre.nl>' . "\r\n";

                        $mail = mail($to, $subject, $message, $headers);

                        if ($mail) {

                            $msg = "Your account has been created, check your email to activate your account.";

                            //header("Location: register.php");  

                        } else {
                            $msg = "Something went wrong, please try again.";
                        }
                }
            }
        }    
    }
}
?>

我想我错过了一些东西,但我现在花3个小时试图解决这个问题。在此先感谢您的帮助。

0 个答案:

没有答案