Many people often ask the question related to form validation. Here is
my style. It just gives the idea; but not exactly what I do in
development.
Comments are welcome.
------------->8---------------------
<?php
//usually header...
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Form Validation</TITLE>
<META http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<STYLE type="text/css">
<!--
p, input,select, table {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 9px;
}
-->
</STYLE>
</HEAD>
<BODY>
<?php
$tst_form = new TestForm();
if ($_POST) //form posted?
{
$tst_form->validateFormInputs($_POST); //extract inputs
if ($tst_form->isValidInputs())
{
$tst_form->addFormInputs();
echo '<P align="center">Values added to the DB</P>';
}
else
echo '<P align="center"><SPAN style="color:#ff0000">Errors found
in inputs</SPAN></P>';
}
//html template....
?>
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="post"
autocomplete="off">
<TABLE width="300" border="0" align="center" cellpadding="5"
cellspacing="0">
<TR>
<TD>Enter Text</TD>
<TD><INPUT name="text" type="text" id="text"
value="<?=$tst_form->getFieldValue('text')?>"> <?=$tst_form->getError('text')?></TD>
</TR>
<TR>
<TD>Check</TD>
<TD><INPUT name="check" type="checkbox" id="check"
value="1"<?=($tst_form->getFieldValue('check')=='1')?'
checked':''?>></TD>
</TR>
<TR>
<TD>Menu</TD>
<TD><SELECT name="menu" id="menu">
<?=$tst_form->populateMenu($tst_form->getFieldValue('menu'))?>
</SELECT></TD>
</TR>
<TR align="center">
<TD colspan="2">
<INPUT type="submit" value="Submit">
</TD>
</TR>
</TABLE>
<?php
//usually footer...
?>
</FORM>
</BODY>
</HTML>
<?php
/*-------------------------------------------------------------------------
* class TestForm - used in this file
*-----------------------*/
class TestForm
{
var $fields_val = array(); //hash array to hold field values
var $errors = array(); //hash array to hold errors
var $expected_vars_and_default_vals = array();
function TestForm() // constructor
{
$this->expected_vars_and_default_vals = array('text' => '',
'check' => 0,
'menu' => 1);
$this->fields_val = $this->expected_vars_and_default_vals;
$this->errors = array('text' => ''); //check only in 'text'
}/*---TestForm()-----*/
function populateMenu($hightlight_o)
{
//menu values... usually fetched from DB
$menu_arr = array( '1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4',
'5'=>'5');
foreach($menu_arr as $key => $value)
if ($key == $hightlight_o)
echo '<OPTION value="'.$key.'" selected>'.$value.'</OPTION>';
else
echo '<OPTION value="'.$key.'">'.$value.'</OPTION>';
}/*---populateMenu()--------*/
function getFieldValue($index)
{
return($this->fields_val[$index]);
}/*---getFieldValue()--------*/
function validateFormInputs($post_arr)
{
//safe extraction of user inputs...
//can also use get_magic_quotes_gpc() if necessary
foreach($this->expected_vars_and_default_vals as
$key=>$default_value)
if (isset($post_arr[$key]))
$this->fields_val[$key]= is_string($post_arr[$key]) ?
htmlentities(trim($post_arr[$key])) : $post_arr[$key];
else
$this->fields_val[$key] = $default_value;
//validation...
$err_begin = '<SPAN style="color:#ff0000">';
$err_end = '</SPAN>';
if ( ! preg_match("/^.+@.+\..+$/", $this->fields_val['text']) )
$this->errors['text'] = $err_begin.'Invalid email'.$err_end;
}/*---validateFormInputs()------*/
function isValidInputs()
{
$ok = true;
foreach( $this->errors as $key => $value)
$ok = ($ok and empty($value));
return($ok);
}/*---isValidInputs()-----*/
function getError($index)
{
return($this->errors[$index]);
}/*---getError()--------*/
function addFormInputs()
{
//stuff to DB
}/*---addFormInputs()---------*/
}/*-----------class TestForm----------*/
?>
-------------8<---------------------
--
"Success = 10% sweat + 90% tears"
If you live in USA, please support John Edwards.
Email: rrjanbiah-at-Y!com