SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    790
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    if all text fields are empty

    hi,

    I have a very simple form with just text fields (apart from submit & reset btns)

    I want to simply test for whether or not ALL text fields in the form are empty.. something like..


    Code:
    if(!isset($_GET[ all text fields ])
    ;-)

    thank you..

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You've already got the right structure.
    && = AND.
    If FieldA is empty AND FieldB is empty AND......
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    790
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh man.. I was hoping not to have to do it like that, was hoping for something slicker....;-)
    (can't you put all values from text fields in an array like you can with values in checkboxes??)

    thank you..

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,598
    Mentioned
    411 Post(s)
    Tagged
    6 Thread(s)
    Yes, you can do that. You could also set up an array to check for errors, and then do something if there's an error ... But if you want to check specifically that all inputs are empty ... StarLion's seems worth considering to me. Why would you need to check for that specifically? (or are we taking you too literally?)

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    the trick is that text fields get sent (and set) even if they're empty. Checkboxes dont get sent if they're unchecked, so it's easy to check checkboxes.

    empty != isset

    Checkboxes checker:
    PHP Code:
    if(count($_POST['checkboxes']) == 0) { //No checkboxes set 
    Textfields are a little harder. Assuming an form-array named textboxes (You can use $_POST if the only things being posted are text fields)
    PHP Code:
    if(array_sum(array_map('strlen',$_POST['textboxes'])) == 0) { //All text fields have an empty value. 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •