user1556433
user1556433

Reputation:

How to show error messages in HTML page in PHP?

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.

Upvotes: 6

Views: 53592

Answers (8)

Karuppiah RK
Karuppiah RK

Reputation: 3964

<?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>

Upvotes: 1

Hassan Sardar
Hassan Sardar

Reputation: 4523

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.

Upvotes: 10

Sina R.
Sina R.

Reputation: 1788

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

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

Upvotes: 2

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

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.

<?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>

Upvotes: 1

Krish R
Krish R

Reputation: 22721

Hope it helps you,

In 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,

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

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

Upvotes: 1

Joke_Sense10
Joke_Sense10

Reputation: 5412

Try this:

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:

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

Upvotes: 1

pratim_b
pratim_b

Reputation: 1210

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

In the login.php

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

Upvotes: 3

Jenz
Jenz

Reputation: 8369

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.

Upvotes: 1

Related Questions