ArticlesReader.com Menu
Newest Articles
Most Viewed Articles
ArticlesReader.com RSS
Submit Article
Login
Signup
Search the articles

Articles Main Categories
Advice
Animals
Automobiles
Business
Career
Communications
Computer Programming
Computers
Entertainment
Environment
Family
Fashion
Finance
Food
Health & Medical
Home & Garden
Humor
Internet Business
Internet Marketing
Legal
Leisure & Recreation
Marketing
Other
Politics
Reference & Education
Religion
Self Improvement
Sports
Technology & Science
Travel
Writing
Subscribe
Receive alert message from us when new articles submitted to our site for free.

Enter your name

Enter your email

Syndicate

















Related Products
Home::CGI

PHP:Form Series, Part 1: Validators & Client-side Validation

Author : Dennis Pallett

Introduction Welcome to the first part of a new two-part series
on the PHP:Form web component. In this part, I will give you an
introduction to PHP:Form, its features, and why it is so useful.
I will also discuss the inbuilt validators that PHP:Form
supports. In the second part I will discuss the more advanced
features of PHP:Form.

What is PHP:Form? PHP:Form is a (new) web component, developed
by TGP PHP Scripts (http://phpscripts.pallettgroup.com). It is
designed to help you create forms with a lot less effort. When
you're creating a new PHP script, you will undoubtedly have to
create forms to allow input to be entered. There is (almost) no
PHP script without forms, and forms usually require solid
validation to make sure there are no security leaks. It would be
nice if those forms are accessible as well, but this often gets
forgotten.

We've all had to create forms again and again. And every time
we've had to write those validation functions. I'm sure you
remember... if (empty($blah)) { echo 'Invalid'; }, etc. After a
while it becomes a really boring and mundane task, and most of
the time we don't do it properly either. One of the hardest
things to do is to return error messages, indicating what is
wrong, and allowing visitors to fix their mistakes, without
having to re-do the complete form. I used to simply tell them to
hit the back-button, after returning the errors, but this is
obviously a not-so-good way of doing it.

Thankfully, that's where PHP:Form steps in. It handles all the
boring parts for you, and does it properly as well.

PHP:Form has support for inbuilt validators, which means you
only have to use a simple HTML-like syntax to add new validation
logic to a form. With these validators also comes automatic
client-side validation. All the necessary JavaScript is created
for you, and there's nothing you need to do. Another remarkable
feature of PHP:Form are so-called "formtypes", which are
basically form templates. These allow you define form templates,
which can be re-used over and over again.

Let's have a closer look at the validation part of PHP:Form; the
validators.

Inbuilt Validators & Client-side Validation Before we look at
the validators, let's first look at the basic PHP:Form syntax.
It has a really simple syntax, because it's basic HTML. To
create a new form, use the tags, like so:

 	... form html goes here ...	


That's all that is really necessary. But you must also tell the
form to display, using PHP, like so:

$_FORMS->display ('example');

Those two things are the only things absolutely necessary to
create and display a new form. Of course, nothing will be
displayed yet because you haven't created any input fields. To
see a simple form in action, have a look at demo 1
(http://www.phpit.net/demo/phpform%20series/demos/demo1.php)

Let's move on to validators now. Validators are used to validate
form fields, and just like the form tags, they are simple html,
for example:

Your error message
here


You can either place validators in between the form tags, or
outside the form tags. If you place them outside the form tags
you must specify the form name (using the 'form' attribute).

A simple form with a validator would look like this:


// Include PHP:Form include ('../phpform.php');

// Begin form: ?>

required="true">

Please
enter your name



Name: value="Go!" />


validate ('example') == true) { // Show
POST'ed values echo '
'; 	print_r ($_POST); 	echo '
';
} else { // Display form $_FORMS->display ("example"); } ?>
View live demo
(http://www.phpit.net/demo/phpform%20series/demos/demo2.php)

As you can see in the code we created a validator that has the
required attribute set to "true". That means that this validator
just checks whether the value of the input field isn't empty.

There are 5 different kinds of validators: - Required: they are
used to make sure an input field isn't empty, like I just
demonstrated.

Please fill in
something


- Numeric: they are used to make sure an input field only
contains numbers, and nothing else.

Please fill in
something


- Regex: they can be used to specifiy a regular expression that
an input field must match.

Please enter 'test'
only.


- Callback: callback validators can take a callback function
that is run on the server-side. That callback function is passed
the value of the input field, and the function must return true
or false. This is used for really advanced validation (and it's
likely you will hardly ever use the callback validator)

Not a valid
e-mail address.validator>


- Name: name validator can be used to display a message or error
only when you want to. They can only be shown when you manually
show them using the trigger_error ('form', 'errorname') method.

This is my custom
error!


Then in PHP: trigger_error ('example', 'mymsg');
?>

When using validators, you will probably want to check if a form
validates or not. To do this, use the validate() method, as seen
in demo 2:

If ($_FORMS->validate('example') == true) { echo 'It
validates!'; } else { echo It 'doesn\'t validate!'; }

Client-Side Validation PHP:Form also automatically generates
client-side validation (JavaScript) when using validators. It
natively supports the required, numeric and regex validators,
but it doesn't (fully) support the callback validator. This
isn't really possible either, because the callback validator
points to a function on the server-side. But if you create a
JavaScript function with the same name as the callback function,
it will work, and it will run the JavaScript function you
created. This gives you great power, and means you can even
using advanced JavaScript functions and Ajax to validate data.

If you would like to see the client-side validation in action,
have a look at demo 2 again, and make sure you have JavaScript
enabled. You will probably notice how fast the errors are
returned, and that no refresh happens at all. That's the
client-side validation.

Conclusion In this first part of the PHP:Form series I have
shown you what PHP:Form is: an extremely neat PHP form
component, that is really useful for building web forms. I have
been using it myself now for a few months, and I still can't get
over how great it is. It has really simplified things, and I can
focus on the important stuff. If you're still in doubt, have a
look at the PHP:Form product page for more information and
demo's (http://phpscripts.pallettgroup.com/phpform/?ref=phpit).

I have also shown you exactly what validators are, and the
different types. Validators are the most important part of
PHP:Form, and you will probably use them in every form. You can
some really interesting things with them, and when you combine a
few validators it's possible to create a extremely secure form.

In the next part I will have a look at "form types", the form
templates of PHP:Form. I will also have a look at setting
default values, using the set_value() method of PHP:Form.

If you're interested in purchasing PHP:Form, don't forget to use
the special PHPit coupon code: phpit PHP:Form product page
(http://phpscripts.pallettgroup.com/phpform/?ref=phpit)

Spam emails More free articles

Related articles


  1. 5 CGI Scripts You Must Use to Turn Your Site Into a Powerhouse
  2. Clever Profit Growth Software
  3. Why Aren't You Using CGI
  4. Use CGI to Automate Your Web Site
  5. CGI: What the Heck Is That?
  6. CGI Security Issues
  7. How to Stop Digital Thieves with CGI
  8. Quick Intro to PHP Development
  9. Better Writing: What Works and What Doesn't
  10. Password Protection and File Inclusion With PHP
  11. Autoresponders With PHP
  12. Track your visitors, using PHP
  13. PHP On-The-Fly!
  14. PHP and Cookies; a good mix!
  15. Screen scraping your way into RSS
  16. Mastering Regular Expressions in PHP
  17. ASP, CGI and PHP Scripts and Record-Locking: What Every Webmaster Needs To Know
  18. Open Source Scripts
  19. this is a test
  20. An Extensive Examination of the PHP:DataGrid Component: Part 1
  21. PHP:Form Series, Part 1: Validators & Client-side Validation
  22. Design an Online Chat Room with PHP and MySQL
More related feeds
Javascript/php Multi Page Form
The dynamic adding of these is done elsewhere - what I want is a flat html/php form that will check for the existence of html elements by id and if it finds them then validate them (both client and server side). ...

PHP Form Validation Script 1.0
A simple, generic form validation script for use in your PHP form processors.

PHP Form Validator 1.0
Adding server side validation is quick and easy using this PHP form validation script. All commonly required form validations are provided. You just have to mention the type of the form validation.

Sending an image to email via PHP form?
Mail posting part starts here ///////// $headers=""; //$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers; // Un comment the above line to send mail in html format $headers4=$y_email; // Change this to change from ...

Adding Form to DB Record in PHPR
The biggest thing that I ran into was issues with Smarty Templates and just inserting a standard PHP Form into them, that was a no-go all the way around for the most part, I did stumble on some ways to do this if anyone is interested ...

Freeware Part 5
New feature: Built-in PHP form processor to make creating forms even more easier (includes support for file upload, anti-hacking and server sided email validation)! - New feature: Form processor also supports the built-in PHP form ...

99 Resources for Web Designers
Firefox extension to validate your HTML (as used within http://validator.w3.org/). 52. Link Checker Checks for broken links in your HTML pages. 53. Firebug Edit, debug, and monitor CSS, HTML, and JavaScript live in any Web page ...

PHP Form Handling
It provides methods for the flexible and structured creation, validation, and display of HTML forms. HTML_QuickForm frees you from doing the grunt work of displaying defaults for form elements, encoding HTML entities, and duplicating ...

FileMakerĀ® API for PHP Tutorial
Discover how it works and how questionnaires are built by creating questions with multiple answers. Your knowledge of the questionnaire.fp7 design will be an integral part of understanding how the tutorial PHP application uses the API. ...

PHP:Form Series, Part 1: Validators & Client-side Validation
In this first part, of a new two-part series, Dennis Pallett has a look at the PHP:Form component. He first discusses the component, and its features. He then has a deeper look at the inbuilt validation of PHP:Form, called validators, ...

 


 

© 2007 articlesreader.com - All Rights Reserved