SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How would I do this?

    I have a bunch of form fields that I want to check if they're empty.

    I could have a bunch of
    PHP Code:
    if (empty($_POST['field1'])
    {
    echo 
    "it was empty";

    could I like loop through all my form and then echo "You didn't fill out $missingfeildname";

    ?

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    FL
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im not sure you can do that...I use these (heres an example):
    PHP Code:
    // Check for a first name.
    if (eregi ("^[[:alpha:].' -]{2,15}$"stripslashes(trim($_POST['first_name'])))) {
    $fn escape_data($_POST['first_name']);
    } else {
    $fn FALSE;
    echo 
    '<p><font color="red" size="+1">Please enter your first name!</font></p>';


  3. #3
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just loop through each value in the post array. ^^
    PHP Code:
      foreach($_POST as $name => $value){
         if(empty(
    $value)){
           
    // do code - $name is the name as in $_POST['name'] and $value is the value entered in the form
         
    }
      
      } 
    Hope that helps!
    Codyrobert.com - Designer and Developer

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    ble
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you guys are all right, but I thought it was like:

    Code:
    if (!$field1){
    echo "Feild is empty";
    }
    Im not sure if that is right. but is it? :s.
    If someone was helpful, give them some 'rep'.

  5. #5
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, use $_POST['field1'] instead of $field1

    Second of all, that WOULD be right, but, you would have to place 10 of those of you had 10 form fields, looping through the $_POST array is easier in that sense..

    The problem is you need different validation for each type of entry. A name you need to check if it consists of only letters, an e-mail adres you need to validate according to it's structure etc etc...

    Quaint Tech
    - Blog on web development and web technology.

  6. #6
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Tintern
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a thought. I always use the test in the form strlen(trim($_POST["name"])) == 0 for text fields just to make sure that a space hasn't been entered where text is required. This only works for text fields of course as Quaint says and, of course, you may need to use isset to check that something like a checkbox has been checked.


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
  •