Page 1 of 1

help me in displaying error infront of the field

Posted: Thu Sep 10, 2009 4:09 am
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

Re: help me in displaying error infront of the field

Posted: Thu Sep 10, 2009 4:49 am
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.