Jump to content

Archived

This topic is now archived and is closed to further replies.

rendrik

If statement issue...

Recommended Posts

I'm having this issue with form validation..
Basically it just skips all my if statements such as:
[code]
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");
[/code]
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.

Share this post


Link to post
Share on other sites
You need to put if statements within {} try:

[code]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");
}[/code]

Share this post


Link to post
Share on other sites
Here it is from the opposite direction... Lemme know...
[code]
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");}

[/code]

Share this post


Link to post
Share on other sites
Instead of multiple "if" statements, you could try using a "switch" statement:
[code]<?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;
    }
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.