Jump to content

Controlling error message location


Eiolon

Recommended Posts

I have an error message that appears if the username or password field has not been filled out. The problem is it always appears in the upper left hand corner of the screen. How would I make it so I can control where in the layout the message appears?

 

<?php // Verify that the username has been entered.
if (empty($_POST['username'])) {
$u = FALSE;
echo 'You must enter a username.<br />';
} else {
$u = escape_data($_POST['username']);
}

// Verify that the password has been entered.
if (empty($_POST['password'])) {
$p = FALSE;
echo 'You must enter a password.<br />';
} else {
$p = escape_data($_POST['password']);
}

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/79306-controlling-error-message-location/
Share on other sites

There are many solutions. Here's one of them-

Put all of you validation code in a function. This function will return a string containing an error if an error occurred. If no error occurred, the function will return boolean true. Then you call the function and get it's returned value into a variable $error. Then, you check:

 

<?php

$error = validation(); //The function that I was talking about

if($error !== TRUE) //An error occurred, print the error in a nice format
{
echo "Welcome to my site!<br>";
echo "<center>".$error."</center>";
die;
}

//Continue your script here... (no error occurred)

?>

 

 

Hope this helps.

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.