T-XU.COM      
Home |
  Home>Computer Programming>CGI>
PHP:Form Series, Part 1: Validators & Client-side Validation
By Dennis Pallett        [Hits: 10021]



Introduction Welcome to the first part of a new two-part serieson the PHP:Form web component. In this part, I will give you anintroduction to PHP:Form, its features, and why it is so useful.I will also discuss the inbuilt validators that PHP:Formsupports. In the second part I will discuss the more advancedfeatures of PHP:Form.

What is PHP:Form? PHP:Form is a (new) web component, developedby TGP PHP Scripts (http://phpscripts.pallettgroup.com). It isdesigned to help you create forms with a lot less effort. Whenyou're creating a new PHP script, you will undoubtedly have tocreate forms to allow input to be entered. There is (almost) noPHP script without forms, and forms usually require solidvalidation to make sure there are no security leaks. It would benice if those forms are accessible as well, but this often getsforgotten.

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

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

PHP:Form has support for inbuilt validators, which means youonly have to use a simple HTML-like syntax to add new validationlogic to a form. With these validators also comes automaticclient-side validation. All the necessary JavaScript is createdfor you, and there's nothing you need to do. Another remarkablefeature of PHP:Form are so-called "formtypes", which arebasically 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; thevalidators.

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

 	... form html goes here ...	


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

$_FORMS->display ('example');

Those two things are the only things absolutely necessary tocreate and display a new form. Of course, nothing will bedisplayed yet because you haven't created any input fields. Tosee 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 validateform fields, and just like the form tags, they are simple html,for example:

Your error messagehere


You can either place validators in between the form tags, oroutside the form tags. If you place them outside the form tagsyou 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: ?>

Pleaseenter your name



Name:


validate ('example') == true) { // ShowPOST'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 therequired attribute set to "true". That means that this validatorjust checks whether the value of the input field isn't empty.

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

Please fill insomething


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

Please fill insomething


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

Please enter 'test'only.


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

Not a valide-mail address.validator>


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

This is my customerror!


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

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

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

Client-Side Validation PHP:Form also automatically generatesclient-side validation (JavaScript) when using validators. Itnatively supports the required, numeric and regex validators,but it doesn't (fully) support the callback validator. Thisisn't really possible either, because the callback validatorpoints to a function on the server-side. But if you create aJavaScript function with the same name as the callback function,it will work, and it will run the JavaScript function youcreated. This gives you great power, and means you can evenusing 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 JavaScriptenabled. You will probably notice how fast the errors arereturned, and that no refresh happens at all. That's theclient-side validation.

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

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

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

If you're interested in purchasing PHP:Form, don't forget to usethe special PHPit coupon code: phpit PHP:Form product page(http://phpscripts.pallettgroup.com/phpform/?ref=phpit)
  Top Articles
*CGI Security Issues
*Clever Profit Growth Software
*Why Aren't You Using CGI
*How to Stop Digital Thieves wi
*Open Source Scripts
*5 CGI Scripts You Must Use to
*Quick Intro to PHP Development
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*this is a test
*Screen scraping your way into
*ASP, CGI and PHP Scripts and R
  Related Articles
*Design an Online Chat Room wit
*An Extensive Examination of th
*this is a test
*Open Source Scripts
*ASP, CGI and PHP Scripts and R
*Mastering Regular Expressions
*Screen scraping your way into
*PHP and Cookies; a good mix!
*PHP On-The-Fly!
*Track your visitors, using PHP
*Autoresponders With PHP
*Password Protection and File I


Prev: Phentermine Generics & Brand Names: How To Avoid Prescriptions Riddled With Impu   Next: Generate More Sales in ANY Affiliate Program ¨C Part Six



Home | Site Map | Bookmark this site | T-XU RSS
Copyright 2007 T-XU.com - All Rights Reserved Worldwide.