PHP回显到HTML的某个部分

时间:2016-04-28 23:40:34

标签: php html sql

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

例如。让我们说下面的代码都在一个PHP文件中。我怎么能将php代码中的2个echos回显到评论所在的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>

3 个答案:

答案 0 :(得分:3)

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

然后在你的div里面

<?php

echo $message;

?>

答案 1 :(得分:3)

您可以将文本分配给变量,并在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>

答案 2 :(得分:0)

如果未设置$ message,则会抛出错误,所以为什么不使用:

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

你可以初始化

$message = ""; 
在您的代码中

然后显示Jacob See&amp; amp; rishal