Forum Moderators: coopster

Message Too Old, No Replies

Form and function( ) Question

         

bumpaw

7:59 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



I have a form with 18 fields, but I have 10 required for the user to fill in. This function works to check all 18, but I can't figure out how to rewrite it to check for only 10.


function filled_out_pass($form_vars)
{
// test that each variable has a value
foreach ($form_vars as $key => $value)
{
if (!isset($key) ¦¦ ($value == ''))
return false;
}
return true;
}

jatar_k

8:02 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



since it is a for each loop it will check every single element in the $form_vars array. I am guessing that you call the function like so

$checkvals = filled_out_pass($_POST);

What you need to do is pass it only the values that you want to check. You can make a seperate array from the $_POST array and pass that to the function to check.

bumpaw

9:37 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



It is called with:

if (filled_out_pass($HTTP_POST_VARS))

I almost understand what you are saying, but could you expand slightly for my novice level PHP?

jatar_k

10:39 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



pretty much what I figured

$HTTP_POST_VARS is the pre php 4.1.0 version of $_POST

At the moment you are passing all of the data posted from the form to your empty check function. It returns either true or false and processing continues based on the return value.

What you need to do is only pass the fields you want to check OR make the adjustment in the function. Since the function is so simple you could probably just put it straight in your code.

There are a bunch of ways to do it but I will give you an example. I will use an example with out the function.

$filled_out_pass = true;
// add the names of fields NOT to check
$nocheck = array('fax','cellnum','howhear');

foreach ($HTTP_POST_VARS as $key => $value) {
if (!in_array [ca.php.net]($key,$nocheck) && (!isset($key) ¦¦ ($value == '')))
$filled_out_pass = false;
}

if ($filled_out_pass) {
//continue processing
}

didn't test it but should work unless I messed up my AND(&&) OR(¦¦) logic, which happens. All I added essentially is that it now checks the var against an array of field names you don't want checked.

bumpaw

11:30 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



Thank you very much. If I can't figure it out with that complete explanation I'll give up.:) Probably have to wait till tomorrow to finish with it,since I have a meeting tonight.