如何在PHP中的HTML页面中显示错误消息? [英] How to show error messages in HTML page in PHP?

查看:274
本文介绍了如何在PHP中的HTML页面中显示错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <表单

action =processlogin.phpmethod =post>
< input type =textname =username>
< input type =passwordname =password>
< input type =submitvalue =Login>
< / form>

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

  if(!$ _ POST [username] ||!$ _ POST [password])
{
$ msg =您离开一个或多个必填字段。
echo $ msg;
//header(\"Location:http://localhost/login.php);
}

这段代码检查是否所有的必填字段都被填充。

直到现在一切正常。



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

解决方案

我宁愿使用Jquery验证或基于Ajax的身份验证。但是你仍然可以这样做:



把你的错误信息放入Session中,如下所示:

  $ _ SESSION ['Error'] =您留下了一个或多个必填字段。 

比简单地显示它是这样的:



<$如果(isset($ _ SESSION ['Error']))
{
echo $ _SESSION ['Error']; p $ p>

unset($ _ SESSION ['Error']);






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

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

<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

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.

解决方案

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

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.

这篇关于如何在PHP中的HTML页面中显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆