SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have a inefficient way of checking for valid fields.
    Is there anyone who has a good method of checking for empty fields?

    Currently, mine is like...

    if ($username == '') { XXX }
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That poses a cool question, I came up with this

    PHP Code:
    <?
    //These are the fields that are required
    $req_fields = array("name" => "Misisng Name""email" => "Missing Email");

    if (
    $submit) {
        
    //Check each element of the required fields and make sure its equiv from the for is not blank
        
    foreach($req_fields as $key => $val) {
            if ($
    $key == "") {
                
    //If it is blank add the value from the req fields array to the errors array
                
    $errors[] = $val;
                }
            }
        }
    //If the errors count is greater than 0 then show the errors
    if (count($errors) > 0) {
        foreach(
    $errors as $key => $val) {
            print 
    "$val<br>";
            }
        }

    ?>

    <form action="<?=$PHP_SELF?>">
    Name: <input type="text" name="name" value="<?=$name?>"><br>
    Email: <input type="text" name="name" value="<?=$email?>"><br>
    <input type="submit" name="submit" value="Submit">

    </form>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy' code is cool - I'm currently working on something similar (or planning to work on something similar) that uses objects. However in answer to the original question I use:

    if (!$username)
    {
    // code and stuff
    }

  4. #4
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Freddy,

    You do wonders.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •