Jump to content

Changing error message for form validation


eldan88

Recommended Posts

Hey guys. I have admin page that just says "Welcome. Please enter your username and password".

 

I am trying to change that message if the username and password is empty or if it is not correct.

 

Currently this is what I have ..

$message = "<span style=\"font-size:14px;\"><strong>Welcome </strong></span>!<br>
    Please enter your username and password to log in";

if(isset($_POST['submit'])) {
    
    
     $errors = array();
     $field_name = array('username','password');
      if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) {
    $errors[] = "Please enter a username or password";
      }

If they enter the wrong username and password I have the following

if(mysql_num_rows($result) == 1) {
	//Redirect to Admin Page
// Success	
     } else {
         $errors[] = "Sorry your username or password is incorrect.<br />";
     }


And right above the username and password form I am displaying the following  welcome message

  <div id="login_msg">
    <?php   if(!empty($message)) {echo $message."<br>" ;}

    foreach ($errors as $message) {
        echo $message;
    
 }

Right now its just being appended to the actual $message. How can I completely change the text?? Thanks for your help!

Link to comment
Share on other sites

Only add Please enter your username and password to log in to $message when the form has not been submitted

$message = "<span style=\"font-size:14px;\"><strong>Welcome </strong></span>!<br>";

if(isset($_POST['submit'])) {
     $errors = array();
     $field_name = array('username','password');
     if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) {
          $errors[] = "Please enter a username or password";
     }
}
else {
    $message .= "Please enter your username and password to log in"; // append this to $message (when form has not been submitted yet)
}
Edited by Ch0cu3r
Link to comment
Share on other sites

Chocu3.

 

Thanks for the response, but it doesn't  work  :/. I made the "Welcome" into a string instead of assigning it to a variable $message.

 

The message is not showing when there is an incorrect username/password. Below is the code I have changed

 

 

If username and password is empty

if(isset($_POST['submit'])) {
	
    
     
     $field_name = array('username','password');
      if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) {
    $error = "Please enter a username or password";
      }

If username password is incorrect

if(mysql_num_rows($result) == 1) {
// Redirect to admin section
// Success	
     } else {
         $error = "Sorry your username or password is incorrect.<br />";
     }

// Message to display

    <?php   
    echo "<span style=\"font-size:14px;\"><strong>Welcome User</strong></span>!"."<br>";
       if(!empty($message)) {echo $message."<br>" ;}
     if(!empty($error)) {echo $error."<br>" ;}
    

    
?>
Link to comment
Share on other sites

Here is the whole form to make things easier

if(isset($_POST['submit'])) {
	
    
     
     $field_name = array('username','password');
      if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) {
    $error = "Please enter a username or password";
      }
        
      
if(empty($error)) {

     $username = trim($_POST['username']);
	$password = trim($_POST['password']);
    
$query = "SELECT username,password ";
$query .= "FROM admin ";
$query .= "WHERE username = '{$username}' ";
$query .= "AND password = '{$password}' ";
$query .= " LIMIT 1 ";
$result = mysql_query($query);
confirm_query($result);
if(mysql_num_rows($result) == 1) {
	//Redirect
// Success	
     } else {
         $error = "Sorry your username or password is incorrect.<br />";
     }

}
    } else {
        $message = "Please enter your username and password to log in";
    }
?>

Output message

    <?php   
    echo "<span style=\"font-size:14px;\"><strong>Welcome User</strong></span>!"."<br>";
       if(!empty($message)) {echo $message."<br>" ;}
     if(!empty($error)) {echo $error."<br>" ;}
    

    
?>
Link to comment
Share on other sites

I looked at your code and you have an array containing two field names and you try to throw that into a $_POST[], (if (empty($_POST[$field_name]) || !isset($_POST[$field_name])) {). This should be if (empty($_POST[$field_name[0]]) || !isset($_POST[$field_name[1]])) { should it not? Otherwise you're seeing if $_POST has a key inside of it with the key as array('username', 'password'). You should also define your variables if they even have a chance of not being defined before being called.

if (isset($_POST['submit'])) {
    $error = ''; // set a blank error message

    $field_name = array('username', 'password');
    if (empty($_POST[$field_name[0]]) || !isset($_POST[$field_name[1]])) {
        $error = "Please enter a username or password";
    }

    if (empty($error)) {

        $username = trim($_POST['username']);
        $password = trim($_POST['password']);

        $query = "SELECT username,password ";
        $query. = "FROM admin ";
        $query. = "WHERE username = '{$username}' ";
        $query. = "AND password = '{$password}' ";
        $query. = " LIMIT 1 ";
        $result = mysql_query($query);
        confirm_query($result);
        if (mysql_num_rows($result) == 1) {
            //Redirect
            // Success    
        } else {
            $error = "Sorry your username or password is incorrect.<br />";
        }

    }
} else {
    $message = "Please enter your username and password to log in";
}
Edited by Joshua F
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.