473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[script]Form related

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')?>">&nbsp;<?=$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
Jul 17 '05 #1
0 1549

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lukelrc | last post by:
Hi, I have a form containing (among other things) date fields for which i have a date validation script. What i need to happen is when the user presses the submit button the date validation...
3
by: Catalin Lungu | last post by:
Hi, I would like to call a python script from a link, something like this: <a href="midir/file1.rar" onclick="python_script?param1='cad1'&param2='cad'">Download file1</a> What is the correct...
1
by: lakshya | last post by:
how to disable java script form vb code which is written on masterpage
2
by: Mr B | last post by:
Hi all, I am desperately seeking some guidance/help for this project. I have designed a form using html and it is designed to collect feedback from a user. It is now on a web server, but I...
4
by: Angel | last post by:
I have an html form with checkboxes in different rows. The functionality is that of changing the position of the checkboxes through DOM. When I try to save the form after the changes, firefox...
0
by: Suresh.Eddala | last post by:
Hi, I am trying to do Form validation on client side by using atlas client-centric script. Grouping all the field validation by using "validationGroup" and on button click event checking the...
0
saranjegan
by: saranjegan | last post by:
Hello, Am running a CGI script programmed in C in my apache server , the code is throwing some sorts of error with 500 internal server error , from error log i came to know its...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.