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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.