Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation

Status
Not open for further replies.

buzzt

Programmer
Oct 17, 2002
171
CA
I am processing my forms with a submit.php page. On the submit page, I would like to find a way to produce a message telling which form field they have forgotten to fill out. As it is, I am using this function to tell whether or not the form fields have been filled out. I would like to expand on this.

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


I call it up like this:

if (!filled_out($HTTP_POST_VARS))

Then I return the error message. I tried to make an array of the HTTP_POST_VARS, but was unsuccessful (I'm pretty sure I did not write it correctly).

Any ideas?
 
I can return the missing data like this:

[if (!filled_out($HTTP_POST_VARS))
{
echo &quot;The following field(s) must be filled in:<br>&quot;;
if (!$value1) echo &quot;<br>Value 1&quot;;
if (!$value2) echo &quot;<br>Value 2&quot;;
if (!$value3) echo &quot;<br>Value 3&quot;;
}


but, I would like the same results without having to type in if (!$value1) echo &quot;<br>Value 1&quot;; each time.
 
First of all, you probably want your script to check all of the fields and find all missiong data, not just quit when it finds the first one.

You probably also want a user-readable list of the names of the offending form fields. Take a look at this variant of your code:

Code:
<?php

function filled_out($form_vars)
{
	$retval = TRUE;
  	// test that each variable has a value
  	foreach ($form_vars as $key => $value)
  	{
    	if (!isset($key) || ($value == &quot;&quot;))
    	{
    		if ($retval === TRUE)
    		{
    			
        		$retval = &quot;The following form fields must be filled out:<br>&quot;;
        	}
        	
        	$retval .= $key . '<br>';
        }
	}
	return $retval;
}

$return = filled_out($HTTP_POST_VARS);
if ($return !== TRUE)
{
	print $return;
}
else
{
	print &quot;yes&quot;;
}
?>

Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi,

Check this out, it's a script I made for a form mailer and it checks to see if the fields are filled in or not. If the fields aren't filled in then it shows another input field and all those that are filled in get passed as hidden fields and shown to the user then get reposted once the correct fields are filled in. This validates email addresses to be correct syntax and is a good method of holding all the typed data without making someone risk hitting the back button and losing all the text they just input.

FAQ434-2909

Hope this helps!

relax.gif


Keep up to date with my 2003 NHL Playoffs Wallpaper
 
I get an error message with this change.

Warning: Invalid argument supplied for foreach()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top