Re: Required fields in a form?

From: Date: Fri, 13 Oct 2000 23:51:04 +0000
Subject: Re: Required fields in a form?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20107@lists.php.net to get a copy of this message
$required = array('fname','lname','wphone'); // 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; } } if(strlen($f['button']) && !($errors = check_for_errors($f))){ // add a line } --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 > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net /*****************************\ * Joe Stump * * www.Care2.com * * Office: 650.328.0198 * * Extension: 122 * \*****************************/ http://www.miester.org/Joe/Stump/Joe_Stump_resume.html -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GB/E/IT d- s++:++ a? C++++ UL++$ P+ L+++$ E----! W+++$ N+@ o? K? w---! O-@ M+@ V-! P(++) PE(+) Y+@ PGP+++@ t+@ 5? R-! tv@ b+ DI++@ D(++++) G++@ e+@ h@ r+! z(+++++**)! ------END GEEK CODE BLOCK------

« previous php.general (#20107) next »