|
The HTML markup language has build in codes to provide forms. In forms the user can input fields fill out, in text fields of several lines text enter, from lists of entries selects and so forth.If the form is filled out, the user can click on a Button, in order to process the form. The form contains special tags to indicate what happens with the data of the filled out form. For example send the data by E-Mail or process the data with a CGI program on the server.Generally, the major problem is the form validation.
php form validation tutorialThe HTML markup language has build in codes to provide forms. In forms the user can input fields fill out, in text fields of several lines text enter, from lists of entries selects and so forth.If the form is filled out, the user can click on a Button, in order to process the form. The form contains special tags to indicate what happens with the data of the filled out form. For example send the data by E-Mail or process the data with a CGI program on the server.Generally, the major problem is the form validation. So what is form validation ?Form validation the process that occurs after the user clicks the submit button and before the form data processed by a PHP, JAVA or CGI program. In this tutorial, you'll learn how to intercept the submission and pass the form data through one validation tests. At the end of this tutorial, you´ll have a complete form validation framework in php wich can be seemless integerate in existing web applications. html forms howtoConstructing a form is quit more easy as it appears first. If you've familiar with HTML, then you'll be quick at learning setting up forms. Because forms just some special HTML tags embedded right in a ordinary HTML page. Forms start with the <FORM> tag. All form elements embedded between the opening <form> and closing </form> tag. The area between the form tags consists of html tags. <form parameter> form elements </form> The form tag has two important attributes: action="handlerpage" This Attribute specifies the url which handles the input from the user. Usually, this will be a php, jsp or perl script or even an old school CGI program, which processes the data supplied by the user. method ="GET|POST" In simple terms, if you use GET method, the input values are passed as part of the URL. If it is POST, the information is sent to the server as part of the data body and will not be visible in the URL box in the user's browser. If you don't specify the method, GET is taken by default. For a complete reference take a lock at the HTML 4.01 specification. Look at this small example: <form method="post" action="process.php"> Name: <input name="name"><br /> Email: <input name="email"><br /> <input name="submit" value="SUBMIT"> </form> This example asks for a user name and e-mail address and submit the data to a php script called process.php. For further help on html forms see the html form tutorial from Ray Fischer JavaScript form validationForm validation with JavaScript is a self-suggestion way to validate form data. To understand the difference from JavaScript validation to the active-form framework, I´ll explain in short how to validate forms with JavaScript. Form validation with JavaScript is called client side validation, because the validation is done at the client side. Validating forms on the client lightens the load on the server and allows you to check the form data before it is sent for processing. Example: function validator() { var obj = document.example; if (obj.aTextField.value == "") { alert("Please enter a value for the \"aTextField\" field."); obj.aTextField.focus(); return false; } return true; }
<form method="post" name="example" onsubmit="return validator();" action="processor.php"> <input type="text" name="aTextField"> <input type="submit" value="Save"> <input type="button" value="Cancel"> </form> For this example, I wrote a JavaScript function validator(). The purpose from validator() is to check if aTextField is empty. The form example executes the JavaScript function validator() before the default action processor.php is called. Unfortunately, writing client-side validation in this way is error prone and intricately. A little relief is a generic reusable validation script. For example the JavaScript validation framework from Prasanth M J. So what is the snag ? - JavaScript stays on the client side and depends on users browser. You can test your form with two or three browser but never for all.
- Particularly power users turn of JavaScript
- Validation is limited to client side data, database lookups or height sophisticated constraints are not possible.
Server-side validationIn contrast to client side form validation, the server side form validation will be done after the form is submitted. If one or more form fields invalid, well´ll have to show the input form, together with an error message, again. At the beginning I defined the major requirements for a generic form validation framework. It is called: active-forms The active-forms framework meets three conditions: - The form validation should by browser independent.
- Seamless integration in existing web applications (JAVA).
- Easy to use constraint language with out the need to modify the framework.
|