Client-Side Form Validation (Usually With Javascript Embedded in The Web Page)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Things to do

Before sending the data to the server, data input in the form must be validated that the entered data are correct; It can be done by Java sacript; Java Script cannot be used to store or save form results in the website; Client side Validation of the form to check whether the fields are filled;

Client-side form validation (usually with JavaScript embedded in the Web page)

There are two methods to send form data to a server. GET, the default, will send the form input in an URL, whereas POST sends it in the body of the submission. The latter method means you can send larger amounts of data, and that the URL of the form results doesn't show the encoded form.

PHP helps in how to process the input in the server;


With PHP you are not limited to output HTML. PHP's abilities includes outputting images, PDF files and even Flash movies (using libswf and Ming) generated on the fly. You can also output easily any text, such as XHTML and any other XML file. PHP can autogenerate these files, and save them in the file system, instead of printing it out, forming a server-side cache for your dynamic content.

Dealing with Forms


One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts. Please read the manual section on Variables from external sources for more information and examples on using forms with PHP. Here is an example HTML form: Example #1 A simple HTML form
<form action="action.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your age: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form>

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, theaction.php page is called. In this file you would write something like this: Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>. You are <?php echo (int)$_POST['age']; ?> years old.

A sample output of this script may be:


Hi Joe. You are 22 years old.

Apart from the htmlspecialchars() and (int) parts, it should be obvious what this does. htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page. For the age field, since we know it is a number, we can just convert it to an integer which will automatically get rid of any stray characters. You can also have PHP do this for you automatically by using the filter extension. The $_POST['name'] and $_POST['age']variables are automatically set for you by PHP. Earlier we used the $_SERVER superglobal; above we just introduced the $_POST superglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET superglobal instead. You may also use the$_REQUEST superglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data. Also see theimport_request_variables() function. You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them. We also have a short introduction to handling data received from XForms in our features section.

I was so shocked that some servers have a problem regarding the Submit Type Name and gives a "Not Acceptable error" or Error 406. Consider the example below : <form action="blah.php" method="POST"> <table> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="Submit_btn" id="Submit_btn" value="Send"> </td> </tr> </table> </form> This very simple code triggers the "Not Acceptable Error" on PHP Version 5.2.5 and Apache 1.3.41 (Unix) Server. However to fix this below is the right code: <form action="blah.php" method="POST"> <table> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="Submitbtn" id="Submit_btn" value="Send"> </td> </tr> </table> </form> The only problem that took me hours to find out is the "_" in the Submit Button. Hope this help!

2)[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"] Be careful, when using and processing forms which contains <input type="image"> tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server. Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them.

3) According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.
You should use the GET method when your form is, well, getting something off the server and not actually changing anything. For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.

You might also like