Re: Required fields in a form?
| From: | Joe Stump | Date: | Sun, 15 Oct 2000 05:17:57 +0000 |
| Subject: | Re: Required fields in a form? | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-20217@lists.php.net to get a copy of this message | ||
At the top (if the code is working fine) type:
error_reporting(0);
--Joe
On Sat, Oct 14, 2000 at 06:01:42PM -0700, Erica Douglass wrote:
> Agh... still having problems...
>
> Alright. Following Joe's example, I rewrote the code to get the required
> fields from the database. That works fine. My problem is that when a field
> is blank, nothing is written into the value part of the array for that key,
> so I get a PHP warning when trying to submit my form. For reference
> purposes, $f is my form field array. $f works just fine. It just doesn't
> set values for blank fields.
>
> Here is my code:
>
> $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);
> };
>
> function check_for_errors($f){
> while(list($key,$val) = each($f)) {
>
> if(!strlen($val) && in_array($key,$required_fields)) {
> $errors[$key] = '**Required';
> }
> }
>
> if(is_array($errors)){
> # temporary messages
> print "there are errors";
> }
> else {
> print "no errors found!";
> };
>
> };
>
> This error message pops up for every field that is blank: Warning: Wrong
> datatype for second argument in call to in_array() in /path/to/file on line 139
>
> I tried the suggestion listed in the PHP manual to declare the array before
> adding values to it, but that returned no output.
>
> I have everything else working. If I can get this one thing fixed, it's on
> to bigger and better problems. ;) Can someone help me out? (I do have plans
> to post some free scripts when this is all done, if that is any sort of
> motivator!!)
>
> Erica
>
> At 06:54 AM 10/14/2000 -0700, Joe Stump wrote:
> >Ok then - just build the $required array from your DB - the code will work
> >just the same. So before you call check_for_errors create the array like
> >this:
> >
> ><?
> >
> >
> > while($row = mysql_fetch_array($r)){
> > // $is_required is your test
> > if($is_required){
> > $required[] = $row['required_field'];
> > }
> > }
> >
> >?>
> >
> > > 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
> > >
> > >
> > > --
> > > 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------
/*****************************\
* 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------