将来自 PHP 的错误消息放置到 HTML 中的特定位置

Placing Error Message from PHP to specific place in HTML

我已经创建了一个登录系统。我的 PHP 与我的 HTML 位于不同的脚本文件中。 我想在登录表单的特定位置显示特定的错误消息。我该怎么做呢? 这是我的PHP,你会在代码底部看到我的错误信息。

<?php
// start session 
session_start();

// link textboxes to variables
$email      =   $_REQUEST['txt_Email'];
$pword      =   $_REQUEST['txt_Password'];

// connecting to database (host, username, password, db_name)
$db = new mysqli("localhost","dbuser","dbp@55word","db_OnlineBookClub");

// checking for database connection errors
if(mysqli_connect_errno()) {
    echo "Error Connecting To Database";
    exit;
}
       
// building the query with CRUD statement
$result = $db->query("SELECT name, email, pword FROM tbl_userInfo 
    WHERE email = '$email' AND pword = '$pword' ");

// retrieving number of rows from database
$row_count = $result-> num_rows;

// checking if the query ran successfully
if($row_count > 0) {
    for($i = 0; $i < $row_count; $i++) {
        $row = $result-> fetch_assoc();
        
        // set name as session object 
        $_SESSION['name'] = $row['name'];
    
        header("Location: Home.php");
        exit;
    }
} else {
    // if error occurs
    $errorMsg   =   "Could not find account. Please register.";
}
// closing the database connection
$db->close();
?>

这是我的 HTML,您会在底部看到错误消息。我希望它显示在 table.

的那个单元格中
<html>
<head> 
    <title>Login</title>
    <style>
        body {
            /* Background image */
                background-image: url(Backgrounds/login.jpg);
                background-repeat: no-repeat;
                background-size: cover;
        }
        /* login form table */
        #tbl_login {
            width:40%;
            align-content:center;
            background-color: #FFFFFF;
            margin-top: 300px;
            align-content: center;
            text-indent: 20px;
        }
        .clm {
            color: #606060;
            font-size: 17px;
            font-family: Century Gothic;
            font-weight: bold;
            padding: 10px;
        }
    </style>
    <link rel="stylesheet" type="text/css" href="external.css">
</head>
<body>
    <table id="tbl_login" align="center">
    <form action="LoginProcess.php" method="POST">
        <tr><td colspan="2" class="clm"><p></p></td></tr>
        <tr>
            <td class="clm">Email:</td>     <td align="left"> <input type="text" name="txt_Email" placeholder="johndoe@gmail.com"/> </td>
        </tr>
        <tr>
            <td class="clm">Password:</td>  <td align="left"> <input type="text" name="txt_Password" placeholder="p@ssw0rd"/> </td>
        </tr>
        <tr>
            <td colspan="2" class="clm" align="center"> 
            <!-- Go to Forgot Password page to change password -->
            <a href="ForgotPW.php"> Forgot Password </a> </td>
        </tr>
        <tr><td class="clm" align="right"> 
            <!-- Button to log in -->
            <input type="submit" name="btn_Login" class="tealbutton" value="Log In"/>
            </td>
            <td class="clm" align="left">
            <!-- Button to log in -->
            <input type="submit" name="btn_Register" class="greybutton" value="Register"/>
            </td>
            </tr>
        <tr><td colspan="2" class="clm"><p> 
                <p id="errorMsg" class="error"> 
                    <?php 
                        echo $errorMsg; 
                    ?> 
                </p></td></tr>
    </form>
    </table>
</body>

请注意:这只是一个作业。不用担心安全问题

如果 'html' 文件是一个实际的普通 .html 文件,您无法执行其中的 php 代码。

假设它是一个 .php 文件,一种可能是在 html 部分之前导入登录脚本。通过这种方式,$errorMsg 变量对页面可见(您还必须在 else 块外将其定义为 null 或空字符串,或者在尝试输出之前检查其是否存在)。
首次加载页面时,通过检查是否从表单接收数据来避免执行登录块。

将两个脚本分开的另一种可能性是在出现错误时重定向回表单页面。在这种情况下,您可以将错误消息(或错误代码)作为查询参数传递或将其保存在会话中。
在第一种情况下,您检查 GET 参数是否存在并输出它。
在第二种情况下,您检查会话密钥是否存在,将其保存在局部变量中或将其输出,然后取消设置会话密钥。

作为旁注,我建议在响应 POST 请求进行位置重定向时使用 303 状态代码。
示例:header("Location: Home.php", true, 303);