BIS 3043 Lecture 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Form Handling

•A form is an area that can contain form elements.

•Form elements are elements that allow the user to


enter information (like text fields ,drop-down menus,
radio buttons, checkboxes, etc.) in a form.

•A form is defined with the <form> tag.


Form Handling In HTML
<form>
<input>
<input> Input
The most used form tag is the <input>
</form> tag. The type of input is specified with
the type attribute. The most commonly
used input types are explained below.
1. Text Fields

Text fields are used when you want the


user to type letters, numbers, etc. in
a form.
<form>
First name:
<input type="text" name="firstname">
<br>
Last name:
<input type="text" name="lastname">
</form>
Radio Buttons
Radio Buttons are used when you want the user to
select one of a limited number of choices.

<form>
<input type="radio" name="sex" value="male"> Male
<br>
<input type="radio" name="sex" value="female"> Female
</form>
Checkboxes

Checkboxes are used when you want the user to select one
or more options of a limited number of choices.

Example-20

<form>
I have a bike:
<input type="checkbox" name="vehicle" value="Bike">
<br>
I have a car:
<input type="checkbox" name="vehicle" value="Car">
<br>
I have an airplane:
<input type="checkbox" name="vehicle" value="Airplane">
</form>
The Form's Action Attribute and the Submit Button

<form name="input" action="html_form_action.asp"


method="get">
Username:
<input type="text" name="user">
<input type="submit" value="Submit">
</form>
Example -22

<body>

<form >
<select name="cars">
<option value="800"> Maruthi 800</option>
<option value="Alto">Maruthi ALTO</option>
<option value="Wagonor">Maruthi Wagonor</option>
<option value="Swift">Maruthi Swift</option>
</select>
</form>

</body>
</html>
PHP Form handling
The most important thing to notice when dealing with HTML forms and
PHP is that any form element in an HTML page will automatically be
available to your PHP scripts.

<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html> Welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html
The $_GET Variable

The $_GET variable is an array of variable names and values sent by the
HTTP GET method.

The $_GET variable is used to collect values from a form with method="get".
Information sent from a form with the GET method is visible to everyone (it
will be displayed in the browser's address bar) and it has limits on the amount
of information to send (max. 100 characters).

The $_POST Variable


The $_POST variable is an array of variable names and values sent by
the HTTP POST method.

The $_POST variable is used to collect values from a form with


method="post". Information sent from a form with the POST method is
invisible to others and has no limits on the amount of information to send.
The $_REQUEST Variable

The PHP $_REQUEST variable contains the contents of both


$_GET, $_POST.
The PHP $_REQUEST variable can be used to get the result from
form data sent with both the GET and POST methods.

Welcome <?php echo $_REQUEST["name"]; ?>.<br />


You are <?php echo $_REQUEST["age"]; ?> years old!
Server Side Includes

You can insert the content of a file into a PHP file before the server
executes it, with the include() or require() function

The two functions are identical in every way, except how they
handle errors.

The include() function generates a warning (but the script will


continue execution) while the require() function generates a fatal
error (and the script execution will stop after the error).

These two functions are used to create functions, headers,


footers, or elements that can be reused on multiple pages
The include() Function
 The include() function takes all the text in a specified file and
copies it into the file that uses the include function.

Example

<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>
<html>
<body>
Menu.php <a href="default.php">Home</a> |
<a href="about.php">About Us</a> |
<a href="contact.php">Contact Us</a>
</body>
</html>

<?php include("menu.php"); ?>


<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>
The require() Function

 The require() function is identical to include(), except that it


handles errors differently.
 The include() function generates a warning (but the script will
continue execution) while the require() function generates a fatal
error (and the script execution will stop after the error).

You might also like