Jump to content


Photo

if/else/dowhile


  • Please log in to reply
3 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 29 May 2006 - 04:16 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 kenrbnsn

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

Posted 29 May 2006 - 04:38 PM

Please post your code.

Ken

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 29 May 2006 - 04:43 PM

I wasn't really speaking about a specific section of code, I was trying to learn for knowledge purposes, for when I start doing major applications, more of, just a general question.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 May 2006 - 04:51 PM

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:
<?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>
Note the validation checks used in the above script are basic which are there just to demostrate what I mean.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users