Forum Moderators: coopster

Message Too Old, No Replies

Control Flow logic

problem with IF statement

         

dbarasuk

3:23 am on Apr 29, 2008 (gmt 0)

10+ Year Member



Hello
I have a script using $_POST key to make variables of same name like this:
foreach($_PoST as $key => $value)
{
$$key = strip_tags(trim($value));

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

dreamcatcher

6:53 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_POST is case sensitive.

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

Habtom

7:22 am on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Besides to what DC mentioned, I am not sure what is going with this:

$$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.

dbarasuk

3:19 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



To Dreamcatcher:
$_PoST was a typo here but was correctly written in my code i.e $_POST

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

d40sithui

3:49 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



from your error, it seems 'name' is retrieved twice. comment everything out inside the foreach and just echo $key, $$key, and $value to make sure everything is as it should be.while your foreach looks ok, i'd try different variations such as the below or use a different method. $name = strip_tags(trim($_POST['name'])) works just as well.

<?
foreach(array_keys($_POST) as $key){
$$key = strip_tags(trim($_POST[$key]));
}

henry0

3:50 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should read what coopster php mod here
as written a while ago about var var (scroll down to find this tutorial) I bookmarked it for it is well explained
coopster [webmasterworld.com]

dbarasuk

6:16 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



Hello,

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

d40sithui

6:23 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



perhaps when they are compared, one or the other is not yet instantiated.

dbarasuk

9:44 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



printing $_POST shows well that both $pwd and $conf_pwd are all instantiated.

dreamcatcher

8:54 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you echoed both vars to see what their value is?

echo $pwd;
echo $conf_pwd;

dc

dbarasuk

12:19 am on Apr 30, 2008 (gmt 0)

10+ Year Member




System: The following 2 messages were spliced on to this thread from: http://www.webmasterworld.com/php/3638110.htm [webmasterworld.com] by dreamcatcher - 8:56 am on April 30, 2008 (utc 0)


I have a form with several fields. the default field values are computed by PHP. This is to allow a user for instance to see values he's typed in case there is a field to be corrected. The password field is defined as follows: <input type ="password" name="pwd" value="<?php echo $pwd; ?>" size="40">.
$pwd is instatiated in a foreach loop like foreach($_POST as $key => $value)
{
$$key=$_POST[$key];
}

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

ag_47

1:02 am on Apr 30, 2008 (gmt 0)

10+ Year Member



Just a quick thought, do both of your password fields have the same name?

dreamcatcher

8:57 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please don`t start a new thread for the same issue. I`ve spliced this thread with your original post.

dc

d40sithui

2:53 pm on Apr 30, 2008 (gmt 0)

10+ Year Member



although print_r($_POST) will show both of the variables to be valid, your foreach loop only instantiate one variable at a time. now im not sure the order in which is processes, but what if $_POST['conf_pwd'] was instantiated first - before your $_POST['pwd']- then all $conf_pwd will wil not be the same as $pwd because $pwd is still seen as empty.