Jump to content


Photo

If statement issue...


  • Please log in to reply
3 replies to this topic

#1 rendrik

rendrik
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 10 March 2006 - 01:09 AM

I'm having this issue with form validation..
Basically it just skips all my if statements such as:
if(!$_POST["first_name"] || !$_POST["last_name"] || !$_POST["email"] || !$_POST["mailing_street"] || !$_POST["mailing_city"] || !$_POST["mailing_state"] || !$_POST["mailing_zip"])
    header("location: profile.php?reason=info"); 
if($password1 != $password2)
    header("location: profile.php?reason=password");
if(check_email_address($emailaddress) == false)
    header("location: profile.php?reason=email"); 
if($mailingstate == "XX")
    header("location: profile.php?reason=info"); 
And goes right to the database portion where it dutifully updates the person's record with any gibberish they put in.
Interestingly enough, if I put a die("dead"); anywhere in the code, the if statements magically work.
I'm thinking there is some kind of syntax error i'm just not seeing, is there anything I should be looking for? This has had me stumped for awhile.

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 10 March 2006 - 01:25 AM

You need to put if statements within {} try:

if(!$_POST["first_name"] || !$_POST["last_name"] || !$_POST["email"] || !$_POST["mailing_street"] || !$_POST["mailing_city"] || !$_POST["mailing_state"] || !$_POST["mailing_zip"])
{
    header("location: profile.php?reason=info");
}
if($password1 != $password2)
{
    header("location: profile.php?reason=password");
}
if(check_email_address($emailaddress) == false)
{
    header("location: profile.php?reason=email");
}
if($mailingstate == "XX")
{
    header("location: profile.php?reason=info");
}


#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 10 March 2006 - 01:26 AM

Here it is from the opposite direction... Lemme know...
if(isset($_POST["first_name"]) && isset($_POST["last_name"]) && isset($_POST["email"])  && isset($_POST["mailing_street"]) && isset($_POST["mailing_city"]) && isset($_POST["mailing_state"]) && isset($_POST["mailing_zip"])){
    if($password1 !== $password2)
    header("location: profile.php?reason=password");
    if(check_email_address($emailaddress) == false)
    header("location: profile.php?reason=email"); 
    if($mailingstate == "XX")
    header("location: profile.php?reason=info"); 
    }
else
    {header("location: profile.php?reason=info");} 



#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 March 2006 - 01:56 AM

Instead of multiple "if" statements, you could try using a "switch" statement:
<?php
foreach ($_POST as $k => $v) 
    switch ($k) {
        case 'first_name': // here are all the fields that can't be blank
        case 'last_name':
        case 'mailing_street':
        case 'mailing_city':
        case 'mailing_zip':
            if (strip_tags(trim($v)) == '') header('location: profile.php?reason=info');
            break;
        case 'password1':
            if ($v != $_POST['password2']) header('location: profile.php?reason=password');
            break;
        case 'email':
            if (strip_tags(trim($v)) == '') header('location: profile.php?reason=info');
            if (!check_email_address($v)) header('location: profile.php?reason=email');
            break;
        case 'mailing_state':
            if (strip_tags(trim($v)) == '' || $v == 'XX') header('location: profile.php?reason=info');
            break;
    }
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users