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->validateFormIn puts($_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:#f f0000">Errors found
in inputs</SPAN></P>';
}
//html template....
?>
<FORM action="<?=$_SE RVER['PHP_SELF']?>" method="post"
autocomplete="o ff">
<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')?>">&nbs p;<?=$tst_form->getError('text ')?></TD>
</TR>
<TR>
<TD>Check</TD>
<TD><INPUT name="check" type="checkbox" id="check"
value="1"<?=($t st_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_val s = array();
function TestForm() // constructor
{
$this->expected_vars_ and_default_val s = array('text' => '',
'check' => 0,
'menu' => 1);
$this->fields_val = $this->expected_vars_ and_default_val s;
$this->errors = array('text' => ''); //check only in 'text'
}/*---TestForm()-----*/
function populateMenu($h ightlight_o)
{
//menu values... usually fetched from DB
$menu_arr = array( '1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4',
'5'=>'5');
foreach($menu_a rr as $key => $value)
if ($key == $hightlight_o)
echo '<OPTION value="'.$key.' " selected>'.$val ue.'</OPTION>';
else
echo '<OPTION value="'.$key.' ">'.$value. '</OPTION>';
}/*---populateMenu()--------*/
function getFieldValue($ index)
{
return($this->fields_val[$index]);
}/*---getFieldValue()--------*/
function validateFormInp uts($post_arr)
{
//safe extraction of user inputs...
//can also use get_magic_quote s_gpc() if necessary
foreach($this->expected_vars_ and_default_val s as
$key=>$default_ value)
if (isset($post_ar r[$key]))
$this->fields_val[$key]= is_string($post _arr[$key]) ?
htmlentities(tr im($post_arr[$key])) : $post_arr[$key];
else
$this->fields_val[$key] = $default_value;
//validation...
$err_begin = '<SPAN style="color:#f f0000">';
$err_end = '</SPAN>';
if ( ! preg_match("/^.+@.+\..+$/", $this->fields_val['text']) )
$this->errors['text'] = $err_begin.'Inv alid email'.$err_end ;
}/*---validateFormInp uts()------*/
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 0 1562 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 script is run and only
if the date is correct is the form submited.
What i have done so far is:
-Chagned the button type from 'submit' to 'button'
|
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'¶m2='cad'">Download file1</a>
What is the correct sintax for this sentence? Or something similar... I want
to can download the file but at the same time execute the script.
TIA,
|
by: lakshya |
last post by:
how to disable java script form vb code which is written on masterpage
|
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 don't know how to get it to
return results to an email address when the submit button is pressed.
I have tried using the <form method="post"
action="mailto:blahblah@hotmail.com> but this brings up a dialog box
which requires outlook express to...
|
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 doesnt recognize
the checkbox element whose position was changed.
How do i retrieve the value of the checkbox whose position was changed?
| |
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 validationgroup validated or not. On
focus of each field the validation is checking. I am trying to check
for the entire form at button click event and submit the form to server
side.
I have checked couple of post, not much help on it.. can any one...
|
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 premature end of script headers when am supposed to troubleshoot it with various resources such as checking the script headers i can' t able to overcome it , i faced same problem with some example program in books too(even hello world), so i made the same...
|
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 within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |