如何在 PHP 的 HTML 页面中显示错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19722180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:55:50  来源:igfitidea点击:

How to show error messages in HTML page in PHP?

phphtml

提问by Hassan Sardar

I have following login form (login.php) in which I am asking for username and password.

我有以下登录表单 (login.php),我在其中要求输入用户名和密码。

<form action="processlogin.php" method="post">            
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Login">                                
</form>

Following is the code snippet from my processlogin.php file

以下是我的 processlogin.php 文件中的代码片段

if(!$_POST["username"] || !$_POST["password"])
{
    $msg = "You left one or more of the required fields.";
    echo $msg;
    //header("Location:http://localhost/login.php");
}

This code checks whether all the mandatory fields are filled on not. If not, it shows the error message.

此代码检查是否填写了所有必填字段。如果没有,它会显示错误消息。

Till now everything is fine.

到现在一切都很好。

My problem is that, error message is shown in plain white page. I want to show it above the login form in login.php file. How should I change my code to get my functionality.

我的问题是,错误消息以纯白页显示。我想在 login.php 文件中的登录表单上方显示它。我应该如何更改我的代码以获得我的功能。

回答by Hassan Sardar

I would prefer Jquery Validation or Ajax based Authentication. But still you can do it this way:

我更喜欢 Jquery 验证或基于 Ajax 的身份验证。但是你仍然可以这样做:

Put your Error Message in Session like this :

将您的错误消息放入会话中,如下所示:

$_SESSION['Error'] = "You left one or more of the required fields.";

Than simple show it like this:

比简单的显示它像这样:

if( isset($_SESSION['Error']) )
{
        echo $_SESSION['Error'];

        unset($_SESSION['Error']);

}

In this case you can assign multiple messages in different Operations.

在这种情况下,您可以在不同的操作中分配多个消息。

回答by pratim_b

header("Location:http://localhost/login.php?x=1")

In the login.php

在登录.php

if(isset($_GET('x'))){
//your html for error message
}

回答by Joke_Sense10

Try this:

尝试这个:

html:

html:

<form action="processlogin.php" method="post">            
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login"> 
<span>
 <?php if(isset($_GET['msg']))
  echo $_GET['msg'];
  ?>
</span>                               
</form>

php:

php:

if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
header("Location:http://localhost/login.php?msg=$msg");
}

回答by Krish R

Hope it helps you,

希望能帮到你

In processlogin.php,

在 processlogin.php 中,

        if(!$_POST["username"] || !$_POST["password"])
            {
                $msg = "You left one or more of the required fields.";
                $msgEncoded = base64_encode($msg);
                header("location:login.php?msg=".$msgEncoded);
            }

in login.php file,

在 login.php 文件中,

          $msg = base64_decode($_GET['msg']);
            if(isset($_GET['msg'])){

                if($msg!=""){
                    echo $msg;
                }
            }

回答by Jenz

You can display the message in table or span above the form.

您可以在表单上方的表格或跨度中显示消息。

    <span>
    <?php if(isset($_REQUEST[$msg]))
    echo $msg;
    ?>
    </span>
    <form>
    </form>

And also don't echo $msg in the form's action page.

并且也不要在表单的操作页面中回显 $msg 。

回答by TiMESPLiNTER

Use only one page (your login.php) to display the form and also to validate its data if sent. So you don't need any $_SESSION variables and you have all in one and the same file which belongs together.

仅使用一页(您的login.php)来显示表单并在发送时验证其数据。因此,您不需要任何 $_SESSION 变量,并且您将所有变量都放在同一个文件中,这些文件属于一起。

<?php

$msg = null;

if(isset($_GET['send'])) {
    if(!$_POST["username"] || !$_POST["password"]){
        $msg = "You left one or more of the required fields.";

        //header("Location:http://localhost/login.php");
    }
}

?>

<?php echo ($msg !== null)?'<p>ERROR: ' . $msg . '</p>':null; ?>

<form action="?send" method="post">            
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Login">                                
</form>

回答by Karuppiah RK

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(!$_POST["username"] || !$_POST["password"])
{
    $msg = "You left one or more of the required fields.";
    echo $msg;
    //header("Location:http://localhost/login.php");
}
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post">            
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Login">                                
</form>

回答by ncm

use these functions:

使用这些功能:

<?php
session_start();
define(FLASH_PREFIX,'Flash_')

function set_flash($key,$val){
    $_SESSION[FLASH_PREFIX.$key]=$val;
}

function is_flash($key){
    return array_key_exits(FLASH_PREFIX.$key,$_SESSION);
}
function get_flash($key){
    return $_SESSION[FLASH_PREFIX.$key];
}
function pop_flash($key){
    $ret=$_SESSION[FLASH_PREFIX.$key];
    unset($_SESSION[FLASH_PREFIX.$key]);
    return $ret;
}
?>

And when you want to send a message to another page use

当您想向另一个页面发送消息时,请使用

set_flash('err_msg','one field is empty');
header('location: another.php');
exit();

another.php

另一个.php

<html>
.
.
.
<body>
<?php if(is_flash('err_msg')){?>
<span class="err_msg"><?php echo pop_flash('err_msg'); ?></span>
<?php } ?>
.
.
.
</body></html>