Forum Moderators: coopster
if(ereg('name', $key))
{
if(strlen($name) == 0 ¦¦ strlen($name) > 40)
{
$message[] = "Please, make sure your entered a name or that it is no longer than 40 characters.";
}
}
if(ereg('email', $key))
{
if(!empty($email) )
{
// regex to ensure no illegal characters in email address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match the above string pattern.
if(!preg_match($checkEmail, $email))
{
$message[] = "Your email contains invalid character(s).";
}
}
elseif($email == '')
{
$message[] = 'Did you forget to provide your e-mail address?';
}
}
// Other fields checkings follow.
}
The field 'name' is the first in $_POST. Assuming I leave all fields empty and just hit the submit button, the error message of the first field ('name') is mentionned twice while the other error messages are just mentionned the number of times they are defined in the code.
Why is the first error message appear twice when it's not intended to? Is possible to identify that behavior from the line analysis?
Regards
foreach($_PoST as $key => $value)
Was that a typo for $_POST? If not, you need to make it uppercase. Also, when you say 'error message', what error message do you see? Undefined variable messages? It can be good practice to set your error reporting to E_ALL for development.
dc
$$key = strip_tags(trim($value));
You are creating a variable for the keys;
Forexample, if $key was fruit this $$key would give you $fruit. My guess is $$key is a typo. Even then, I think you meant to put it this way:
$key = strip_tags(trim($key));
$value = strip_tags(trim($value));
I might be missing something, but that should be it.
To HABTOM:
$$key is not a typo, it's a way to avoid creating variables of $_POST manually which can be subject to typo errors.
You said that if $key = 'fruit' $$key would be = to $fruit but you contradict yourself later by saying $key =strip_tags(trim($value));
In this case, you'd have: fruit = strip_tags(trim($value)); if as you said $key was equal to 'fruit'. You see yourself that you need the second $ sign to create the variable $fruit. Thus $$key is not a typo. I saw this in a book of PHP i used to read.
Regards
<?
foreach(array_keys($_POST) as $key){
$$key = strip_tags(trim($_POST[$key]));
}
I almost solved the problem , but there is still one bug:
In fact I changed the program logic since $key can take different values depending on where the internal pointer is located during the foreach loop. I use the switch statement like this($_POST has 6 keys):
if(array_key_exists('submit', $_POST))
{
$message=array();
foreach(array_keys($_POST) as $key ) // array_keys() returns the key names of an array, $_POST in this case
{
$$key = strip_tags(trim($_POST[$key]));
switch($key) // switch() is used when a variable can take different possible valuesknown before hand.
{
case 'name':
if(strlen($name) == 0 ¦¦ strlen($name) > 40)
{
$message[] = "Please, make sure your entered a name or that it is no longer than 40 characters.";
}
break;
case 'email':
if(!empty($email) )
{
// regex to ensure no illegal characters in email address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match the above string pattern.
if(!preg_match($checkEmail, $email))
{
$message[] = "Your email contains invalid character(s).";
}
}
elseif($email == '')
{
$message[] = 'Did you forget to provide your e-mail address?';
}
break;
case 'username':
if(strlen($username) < 6 ¦¦ strlen($username) > 15)
{
$message[] = "Username must be between 6 and 15 characters.";
}
// validate username
if(!ctype_alnum($username))
{
$message[] = "Username must be alphanumeric characters with no spaces in it.";
}
break;
case 'pwd':
if(!empty($pwd) && strlen($pwd) < 6 )
{
$message[] = "Password must be at least 6 characters with no space.";
}
elseif($pwd='')
{
$message[]="Password field is empty,";
}
break;
case 'conf_pwd':
if($pwd !== $conf_pwd)
{
$message[]='Your Passwords could not match.';
}
break;
case 'country':
if(empty($country) )
{
$message[] = "Please, indicate in which country you're residing.";
}
break;
}
}
The remaining problem is the fact that the password field and the one to confirm the typed in password do not match no matter how accurate you type the values in both fields (you can see this using print_r($_POST)), you always keep getting 'Your passwords could not match' even though they match.
Note: this is the only remaining error.
waiting for your help.
regards
When the page is redisplayed to make a correction in the fields(or after submit), although I type something in the password field, that field is redisplayed empty while all others contain the values previously typed in.Consequently it cannot pass matching validation with another password field that collects the same password for validation. How can I understand that tricky thing?
However, the confirmation password field is redisplayed with the value previously typed in. How can i explain that mystery?
thank you for your help