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
help me in displaying error infront of the field
Moderator: General Moderators
-
om.bitsian
- Forum Commoner
- Posts: 36
- Joined: Wed Sep 09, 2009 4:13 am
Re: help me in displaying error infront of the field
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
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:
Your username/idNumber check would be something similar to:
Hopefully this gives you a good idea how to accomplish this.
You would have an Associative array which would look like
Code: Select all
$_SESSION['errorArray'] = array( "error_field" => "username" );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";
}
?>
}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;
}
}