Chapter: 12th Computer Applications : Chapter 8 : Forms and Files

HTML Forms

Main objective of PHP and HTML form controls are to collect data from users.



HTML Forms

Main objective of PHP and HTML form controls are to collect data from users. In the web development, user access website or web pages from remote client machine and feed the data to server. These data are collected via HTML form controls like textbox, dropdown box and radio button etc., and sent to server using server side programming like PHP.


Basic HTML Form Controls

The following control are available in HTML forms:

● Text inputs

● Buttons

● Checkbox

● Radio box

● File Select

● Form Tag


Text inputs contain textbox and text area controls. Buttons may contain Submit button, Reset button and Cancel Button. Checkbox is a important feature which  selects more than one value from the HTML form. Radio box is similar to checkbox but one value can be chosen at a time. File select is the best feature to select one file from the local machine to server machine at a time. Form tag is used to mention a method (POST or GET) and control the entire form controls in the HTML document. Refer Figure 8.1 and 8.2


 

PHP Basic Form Handling

When the user keys in the input data in HTML controls and clicks the submit button the request will be generated and reaches a PHP file which is mentioned in the FORM tag under the Action attribute. All the input values are synchronized and sent to the server via POST method or GET method. Method is an attribute of form tag in HTML. Once the data reaches the server, two PHP variables such as $_POST and $_GET collects the data and prepares the response accordingly.

Post Method: The input data sent to the server with POST method is stored in the request body of the client’s HTTP request.

Get Method: The input data sent to the server with POST method via URL address is known as query string. All input data are visible by user after they click the submit button.


Example:

Test.html:

<html>

<body>

 

<form action=”welcome.php” method=”post”> 

Name: <input type=”text” name=”name”><br>

E-mail: <input type=”text” name=”email”><br>

<input type=”submit”>

</form>

 

</body>

</html>

 

Welcome.php:

<html>

<body>

Welcome <?php echo $_POST[“name”]; ?><br>

Your email address is: <?php echo $_POST[“email”]; ?>

</body>

</html>


● In the above example the HTML File contains two Text Box (Name and Email), One Button and one form tag. The remote server PHP file (welcome. php) is mentioned in form tag under the Action Attribute.

● In “Welcome.Php” file, PHP variables such as $_POST and $_GET collects the data and prepares the response accordingly.

● Eventually the user will receive the output response in the client machine’s browser screen.

 

Basic PHP Form Validation

Validation is a process of checking the input data submitted by the user from client machine. There are two types of validation available in PHP. They are as follows,

Client-Side Validation: The input data validations are performed on the client machine’s web browsers using client side scripts like Java script or adding “required” attribute in HTML input tags.

Server Side Validation: After the submission of data, validations are performed on the server side using the programming like PHP, ASP or JSP etc. available in the server machine.

Validation rules for HTML input field

Name (Text Input) : Must contain letters and white-spaces

Email (Text Input) : Must contain @ and .strings

Website (Text Input) : Must contain a valid URL

Radio : Must be selectable minimum one value

Check Box : Must be checkable minimum one value

Drop Down menu : Must be selectable minimum one value

Client Side Validation:

Before sending the data to server side program (PHP) the programmer can write few validations from browser in the client machine. For this validation, we have to add additional “required” attribute in HTML input tag. Refer Figure 8.3.


Example:

<input> required Attribute in HTML

<form action=”welcome.php”>

Username: <input type=”text” name=”name” required>

<input type=”submit”>

</form>

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Applications : Chapter 8 : Forms and Files : HTML Forms |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.