Jump to content

If statement issue...


rendrik

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.
Link to comment
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]
Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.