help me in displaying error infront of the field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
om.bitsian
Forum Commoner
Posts: 36
Joined: Wed Sep 09, 2009 4:13 am

help me in displaying error infront of the field

Post by om.bitsian »

Hi friends
i am working on php validations. in my application 1- 10 text fields are there i.e
name, age , date etc...

please tell me how can i show error message infront of that field and highlight with red color so that user can know that here is a error.....

i.e if i write in php page
Name : 6317432 Error : Please enter character value
Age : 24
date : 09/09/2009

thanks
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: help me in displaying error infront of the field

Post by Weiry »

One way to achieve this, would be through storing any errors that have occurred in a $_SESSION['errorArray'];
You would have an Associative array which would look like

Code: Select all

$_SESSION['errorArray'] = array( "error_field" => "username" );
The error would be set as an example above when your code determines if there is an error for that field or not.
With that, you would then have something similar to:

Code: Select all

Name : <input type='text' name='idNumber' value='<?php print $_POST['idNumber'];?>' />
<?php
if(!empty($_SESSION['errorArray'])){
   if($_SESSION['errorArray']['error_field']=="username"){
      print "*Username Error";
   }
?>
}
Your username/idNumber check would be something similar to:

Code: Select all

 
function login($username,$password){
   $q = "SELECT `password` FROM `table_users` WHERE `username` = '{$username}'";
   $result = mysql_query($q) or dir("Query Error: ".mysql_error()."<br/>SQL Error: ".$q);
   $numRows = mysql_num_rows($result);
   $resultArray = mysql_fetch_assoc($result);
   if($numRows < 1){
      $_SESSION['errorArray']['error_field'] = "username";
      return false;
   }elseif($resultArray['password']!=$password){
      $_SESSION['errorArray']['error_field'] = "password";
      return false;
   }else{
      return true;
   }
}
 
Hopefully this gives you a good idea how to accomplish this.
Post Reply