PHP Echo to HTML 的某个部分


PHP Echo to a certain part of HTML

我最近完成了我的登录系统,但我有一个关于PHP的问题。如何将文本或 html 回显到 html 的某个部分?

例如。假设下面的代码都在一个PHP文件中。如何将 php 代码中的 2 个回显回音回显到注释所在的 html?

因此,如果我显示此页面并且如果他们的登录失败,它将回显"您输入的用户名和密码与我们的记录不匹配。请仔细检查并重试",以在 HTML 中的该注释。

我希望这是有道理的,谢谢!

<?php
    session_start();
    if (isset($_POST['username']) && isset($_POST['password'])) {
        include_once("db.php");
        $username = mysqli_real_escape_string($sqlcon, $_POST['username']);
        $username = strtoupper($username);
        $password = mysqli_real_escape_string($sqlcon, $_POST['password']);
        for ($i = 0; $i < 1000; $i++) {
            $password = hash(sha512, $password . $username);
        }
        $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
        $user = mysqli_query($sqlcon, $userQuery);
        if (mysqli_num_rows($user) > 0) {
            $user = mysqli_fetch_assoc($user);
            $id = $user['id'];
            $databasepass = $user['password'];
            if ($password === $databasepass) {
                $_SESSION['username'] = $username;
                $_SESSION['id'] = $id;
                header("Location: admin.php");
            } else {
                echo "The username and password you entered did not match our records. Please double-check and try again.";
            }
        } else {
            echo "The username and password you entered did not match our records. Please double-check and try again.";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>ADMIN</title>
    </head>
    <body>
        <div>
            <!--ECHO THOSE TWO ECHOS ABOVE HERE-->
        </div>
    </body>
</html>

您可以将文本分配给变量,并在HTML中使用PHP来回显它。

<?php
    session_start();
    if (isset($_POST['username']) && isset($_POST['password'])) {
        include_once("db.php");
        $username = mysqli_real_escape_string($sqlcon, $_POST['username']);
        $username = strtoupper($username);
        $password = mysqli_real_escape_string($sqlcon, $_POST['password']);
        for ($i = 0; $i < 1000; $i++) {
            $password = hash(sha512, $password . $username);
        }
        $userQuery = "SELECT * FROM users WHERE username = '" . $username . "' LIMIT 1";
        $user = mysqli_query($sqlcon, $userQuery);
        if (mysqli_num_rows($user) > 0) {
            $user = mysqli_fetch_assoc($user);
            $id = $user['id'];
            $databasepass = $user['password'];
            if ($password === $databasepass) {
                $_SESSION['username'] = $username;
                $_SESSION['id'] = $id;
                header("Location: admin.php");
            } else {
                $result = "The username and password you entered did not match our records. Please double-check and try again.";
            }
        } else {
            $result = "The username and password you entered did not match our records. Please double-check and try again.";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>ADMIN</title>
    </head>
    <body>
        <div>
            <?php echo $result; ?>
        </div>
    </body>
</html>

将代码中的echo更改为变量,例如。 $message = "......";

然后在您的div 内部

<?php
echo $message;
?>
如果未

设置$message则会抛出错误,那么为什么不使用:

<?=isset($message)?$message:"";?>

的您可以初始化

$message = ""; 

然后按照 Jacob See 和 rishal 的规定显示$message/$result