Re: Required fields in a form?

From: Date: Sat, 14 Oct 2000 04:34:52 +0000
Subject: Re: Required fields in a form?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-20120@lists.php.net to get a copy of this message
At 04:51 PM 10/13/2000 -0700, Joe Stump wrote:
$required = array('fname','lname','wphone');
The values cannot be hard-coded. The database specifies which ones are supposed to be required. (I'm not going to rewrite any PHP just so someone can add a form field.) Note that in the sample I pulled from the database below, wphone is not required. Therefore, a database call to see which rows are required is necessary. Perhaps a while ($row == mysql_fetch_array($result)) array_push is necessary here. Suggestions?
// f is your form array - so inputs would be // named f[fname], f[lname], f[wphone] function check_for_errors($f){
    while(list($key,$val) = each($f)){
        if(!strlen($val) && in_array($key,$required)){
            $errors[$key] = '**Required';
        }
    }
    if(is_array($errors)){
        return $errors;
    }else{
        return 0;
    }
}
Below I use $HTTP_POST_VARS. Is it necessary to have these values in an array called $f as well?
if(strlen($f['button']) && !($errors = check_for_errors($f))){
    // add a line
}
Still being somewhat of a newbie at this, I don't understand what this bit of code is doing. Is the "add a line" part where I am supposed to print my error message? BTW, I put the form values in an array called $add and tried this code, and I still can't get it to work. I'm stuck somewhere on this part:
        $query = "select field_name from form_fields where required=\"yes\"";
        $result = mysql_query ($query) or die ("The server is down. Here's what MySQL said: " . mysql_error());
        $required_fields = array();
                while ($row == mysql_fetch_array($result)) {
                        $field_name = $row["field_name"];
                        array_push ($required_fields, $field_name);
                };
The while loop does not seem to work, although MySQL returns 12 row values from the query. Still lost... thanks in advance for any help any of you can provide. Rewrite suggestions are welcome as well. A Very Exhausted Erica
--Joe On Fri, Oct 13, 2000 at 04:33:45PM -0700, Erica Douglass wrote: I am trying to build a form that checks if certain required fields are filled out. Not all fields are required, hence my problems. The form calls from a table called "form_fields" that, very simplified, looks like this:
field_name    |       required
__________________________
fname         |       yes
lname         |       yes
wphone        |       no
The form is pulled from the same table. Form value names are evaluated as field_name from the database. Hence, a text field asking for the user's first name looks like this when initially called from the database: <input type="text" name="fname" size="30" value=""> The code I am using to check for required fields looks like this:
      # check to make sure required fields are filled in
      $query = "select field_name from form_fields where required=\"yes\"";
      $result = mysql_query ($query) or die ("The server is down. Here's what
MySQL said: " . mysql_error());
      $required_fields = mysql_fetch_array($result);
              while(list($key,$val) = each($required_fields)) {
                      if ($HTTP_POST_VARS[$key] == "") {
                              echo "$key is blank!";
                      }
                      else {
                              echo "$key is not blank!";
                      };
              };
I am using the echo statements as placeholders for now. This code, when put to use, gives back the following output (regardless of whether the form fields are actually blank or not): 0 is blank!field_name is blank! What on earth am I doing wrong? I have a feeling that I have the right idea, but that my code is off. Please help! :) Erica


« previous php.general (#20120) next »