Jump to content

if/else/dowhile


Ninjakreborn

Recommended Posts

Ok this is what I am trying to figure out, I don't think you can use break when it's with if, elseif, and other like control structures.
What I am wanting to do, is come up with another way for validation, I am trying new things.
I wanted to put all the form fields into an array and compare them all the a blank value but it didn't work out, so now I am wanting to run together a series of if statements.
and for each one taht doesn't match have it say
"You have n ot filled in the first name field"
"you have not filled in the last name field"
ex cetera
but it keeps checking the first one, and exiting the script, I want it to check them all for blank values, and return ALL the messages for those, THEN if it doesn't pick up any errors when checking through the fields, then I want it to check for the email and verify email and see if there the same, seperately. IF it doesn't pick that one up THEN move on to start doing my processing, and working with my database, excetera. But I don't know how to make it do a set of things, then move on to another set IF all those are not found. This is what is confusing me.
Link to comment
Share on other sites

Basically what you'll want to do is submit the form to itself. Then do seperate if statments rather than if/elseif stataments.

Then for each statement you check whether the field has been filled in correct, suchh as the username fieled has 5 or more characters in it. If it doesn't then you create a variavale called $error[] which is an array and stores all the errors inside it. Then when you get to the end of all the if statements you use a foreach loop which echos out all the errors above the form.

If there is no errors in the errors array then you can use the data. Heres an example of what I mean:
[code]<?php

if(isset($_POST['submit']))
{
    //check that the username is atleast 5 chars long
    if(strlen($_POST['user']) < 5)
    {
        //setup $error array
        $error[] = "The username choosen is an invalid length, please use a username that is atleast 5 characters";
    }

    //no do a seperate if statement to check whether the email address is valid and matches the secound email address
    if(strpos($_POST['email1'], '@') === false)
    {
        $error[] = "Your email address is invalid, please supply a valid email address";
    }
    elseif($_POST['email1'] != $_POST['email2'])
    {
        $error[] = "The email address supplied does not match!";
    }

    if(isset($error))
    {
        echo "<b>We have detected one or more errors with the information you have supplied:</b>\n<ul>";

        foreach($error as $k => $v)
        {
            echo '<li>' . $v . "</li>\n";
        }

        echo "</ul>\n";
    }
    else
    {
        echo "No errors where found! Validation successful";
        exit;
    }
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  Username: <input type="text" name="user" value="<?php echo (isset($_POST['user']) ? $_POST['user'] : '') ?>" /><br />
  Email Address: <input type="text" name="email1" value="<?php echo (isset($_POST['email1']) ? $_POST['email1'] : '') ?>" /><br />
  Confirm Email: <input type="text" name="email2" value="<?php echo (isset($_POST['email2']) ? $_POST['email2'] : '') ?>" /><br />
  <input type="submit" name="submit" value="Register" />
</form>[/code]
Note the validation checks used in the above script are basic which are there just to demostrate what I mean.
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.