PHP Form Handling
Posted by tutor | Posted in PHP Tutorial | Posted on 09-08-2010-05-2008
0
A form in PhP or HTML is similar to paper forms which are used to fill some vital data and is then submitted for some registration os similar kind of thing.
Similarly, a webform on a webpage allows a user to fill some information in specific fields which is then processed by a server. So forms are a way to send data to a server for some further processing.
PHP contains, by default all the form elements that are used in html.
<html>
<body>
<form action=”data.php” method=”post”>
Name: <input type=”text” name=”username” />
Address: <input type=”text” name=”address” />
<input type=”submit” />
</form>
</body>
</html>
When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called “data.php”:
data.php:
<body>
Welcome <?php echo $_POST["username"]; ?> ! to the world of PHP<br />
You live at <?php echo $_POST["address"]; ?> .
</body>
</html>
Welcome Tutor!
You live at Capital Federal.
Form Validation
Normally, in a good programming practice, the user inputs are validated at the browser level itself. This lessens the burden on the server, which has lot other work to do. Moreover it is relatively faster.
In case database is being used, then the user input should be validated at server level.
A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
