SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Texas
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking "required" fields.

    I may be mistaken, but I thought I had read some tutorial at this site on how to make a form that checks the "required" fields are indeed filled out. I've tried looking on my own, but I haven't come up with much -- perhaps I'm just overlooking it?

    Could someone point me in the right direction?

  2. #2
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you could check for the required fields using JavaScript, and it would also be a good idea to check using PHP, because forms can be easily bypassed. You could probably find the JavaScript version at http://www.dynamicdrive.com. The PHP would be simple:

    PHP Code:
    foreach ($_POST as $k => $v) {
        if (
    $v == '') {
        echo 
    'Empty field: '$k'<br>';
        
    $missingfield 'true';
        }
    }

    if (
    $missingfield == 'true') {
    //put any bottom includes here if necessary
    exit;

    The above script would check all the fields to make sure they were filled out. If you only need certain fields filled out, you could do this:

    PHP Code:
    $required = array('put','required','field','names','here');

    foreach (
    $_POST as $k => $v) {
        if (
    $v == '' && in_array($k$required)) {
        echo 
    'Empty field: '$k'<br>';
        
    $missingfield 'true';
        }
    }

    if (
    $missingfield == 'true') {
    //put any bottom includes here if necessary
    exit;

    Tested and approved.
    Last edited by Haleden; Jul 6, 2003 at 01:35.

  3. #3
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    England
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this:

    http://www.dynamicdrive.com/dynamici...uiredcheck.htm

    Works very well and is customizable.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally don't see the point of using Javascript for checking FORMs nowadays... Spent a bit of time getting it right first time with PHP That and I'm lazy to implement Javascript with Xsl stylesheets...

  5. #5
    $postcount++; koomann's Avatar
    Join Date
    Feb 2003
    Location
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldn't you just make it a required field like this?
    PHP Code:
    if ($_POST['something']=="" || $_POST['else']=="") {
    echo 
    "You forgot.";


  6. #6
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pah, of course you could, but I think the functions I supplied were much more efficient =P. Especially if you are dealing with a lot of POST elements...

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Texas
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, I was incorrect in thinking that there was a sitepoint tutorial that covered this matter?

    I'm almost positive it was one of the articles a few months ago.

    Anywho, I'd rather accomplish this task in PHP rather than Java.

  8. #8
    SitePoint Addict Ghz's Avatar
    Join Date
    Jun 2002
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I liked the method that Haleden used. Thanx

  9. #9
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woo! *does a dance*

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Haleden
    PHP Code:
    foreach ($_POST as $k => $v) {
        if (
    $v == '') {
        echo 
    'Empty field: '$k'<br>';
        
    $missingfield 'true';
        }
    }

    if (
    $missingfield == 'true') {
    //put any bottom includes here if necessary
    exit;

    I would use a Boolean for true (that's what they're for, after all) and also trim() the value before checking it.
    PHP Code:
    foreach ($_POST as $k => $v)
    {
        if (
    trim($v) == ''
        {
            echo 
    'Empty field: '.$k.'<br>';
            
    $missingfield true;
        }
    }

    if (
    $missingfield
    {
        
    //put any bottom includes here if necessary
        
    exit;


  11. #11
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Palm Desert, California
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just happened to have the same problem and the PHP examples work great. I do have one question. How would I go about having it tell the viewer which field is not completed without giving them the field name.

    Example:

    Field name is "firstname". The above code displays this.
    How could I get it to say "First Name" instead.

    I know this is probably a simple thing but I'm still a newbe to PHP. I have tried renaming the fields but that doesn't seem to help.

    Thanks

  12. #12
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Haleden
    ... but I think the functions I supplied were much more efficient =P. Especially if you are dealing with a lot of POST elements...
    they definitly are. You just may run into problems if you want an error message referring to the appropriate field where there's the wrong/missing input, i guess.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  13. #13
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, another aproach ...
    If the form has few fields say less then 10, I use something like this ...

    PHP Code:
    if (!empty($_POST))
    {
        
    $sErrMsg .= ( empty($email) ? "Email is empty.\n" '' )
        . ( !
    verify_email($email) ? "Email is invalid.\n" '' )
        . ( empty(
    $username) ? "Username is empty.\n" '' )
        . ( empty(
    $comments) ? "Comments are empty.\n" '' );
        
    // and so on and so forth ...

        
    if (empty($sErrMsg))          // Check if there were errors
        
    {
            
    // Do some work if no error ....
        
    }

    Ok, the reason, I use this
    1. I can validate the data along side checking for the empty fields (as shown in email). And the validation can be done on any condition, not just field being empty.
    2. I can have the messags generated against the empty field, that I can display to the user.
    3. I can further check whether there were any errors, and to proceed accordingly.

    If you have more variables then say 10, it would be advise to use a function, with possibly arrays ...

    HTH!

  14. #14
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Palm Desert, California
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Haleden
    If you only need certain fields filled out, you could do this:
    PHP Code:
    $required = array('put','required','field','names','here');

    foreach (
    $_POST as $k => $v) {
    if (
    $v == '' && in_array($k$required)) {
    echo 
    'Empty field: '$k'<br>';
    $missingfield 'true';
    }
    }

    if (
    $missingfield == 'true') {
    //put any bottom includes here if necessary
    exit;

    Tested and approved.
    This works great for what I need. I only have 3 required fields. The problem is it gives the field name "Empty field: fname". How can I make it more user friendly and say "Empty field: First Name" instead? Can I assign the same field name 2 different variables and still have it work? I'm new to PHP and everything I have tried causes the email script not to work.

    Any help would be appreciated..

    Thanks

  15. #15
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Palm Desert, California
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have any ideas?

  16. #16
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can try this ....

    PHP Code:
    $req = array(
        
    'put' => 'Put your Field',
        
    'required' => 'Top Case',
        
    'field' => 'lower case'
        
    'names' => 'Or Whatever message',
        
    'here' => 'Here'
    );
    $required array_keys($req);

    foreach (
    $_POST as $k => $v
    {
      if (
    $v == '' && in_array($k$required)) 
      {
        echo 
    'Empty field: '$req[$k], '<br>';
        
    $missingfield 'true';
      }
    }

    if (
    $missingfield == 'true') {
    //put any bottom includes here if necessary
    exit;

    HTH!

  17. #17
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Palm Desert, California
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Amit..

    It works great!!! I would have never thought about an array to do it.. I'm learning something new everyday..

  18. #18
    $postcount++; koomann's Avatar
    Join Date
    Feb 2003
    Location
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by denny
    I'm learning something new everyday..
    Mmm, you're not the only one.

    Trust me!

  19. #19
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, we all learn a littlebit new everyday ..

  20. #20
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, i dig the script, but how can i create a link back to the form with the fields that were initially filled in still filled in.

  21. #21
    This is all Mumbo Jumbo Amit's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Process the data in the same file which contains the PHP and the form

  22. #22
    SitePoint Zealot jadmadi's Avatar
    Join Date
    Sep 2003
    Location
    Jordan
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    $field = stripslashes($_REQUEST['field']);
    if (empty ($field)) {
    $error .= "Field name";
    }

    //then print all errors
    print $error;
    ?>

  23. #23
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raskolnikov
    Hey, i dig the script, but how can i create a link back to the form with the fields that were initially filled in still filled in.
    I usually implement some basic error handling functionality (with return values), like this:

    PHP Code:
    <?php
    if (isset($_POST['submit'])) {
       
    //Check form's validity
       
    foreach ($_POST as $key=>$element) {

       
    //Default empty fields
       
    if (trim($element) == '')
          
    $errors[$key] = 'Please enter a ' str_replace('_'' '$key);
       }

       
    //Specialty values
       
    if (!is_email($_POST['email'])) {
          
    $errors['email'] = 'Please enter a valid e-mail address';
       }

       
    //If errors, redirect back
       
    if (is_array($errors)) {
          foreach (
    $errors as $error)
             
    $error_string .= '&error[]=' urlencode($error);
          foreach (
    $_POST as $key=>$element)
             
    $previous_values .= "&$key=" urlencode($element);
          
    redirect ($_SERVER['PHP_SELF'] . '?' $error_string $previous_values);
    }

    //Otherwise success!
    And in the form:

    PHP Code:
    <form name="contact" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <strong>
        <?php 
            
    if (is_array($_GET['error']))
               foreach (
    $_GET['error'] as $error)
                echo 
    $error '<br />';
            
    ?>
        </strong>
        
    Your Name: <input name="name" type="text" value="<?php echo $_GET['name']; ?>"><br />
    <br />
    Your Email: <input name="email" type="text" value="<?php echo $_GET['email']; ?>">
    <!--...-->
    </form>
    My name is Steve, and I'm a super-villian.


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
  •