UNIT 2 Conti PHP-Copy2
UNIT 2 Conti PHP-Copy2
<form> Elements
The HTML <form> element can contain one or more of the following form elements:
•<input>
•<label>
•<select>
•<textarea>
•<button>
•<fieldset>
•<legend>
•<datalist>
•<output>
•<option>
•<optgroup>
Form Attributes
The Action Attribute
The action attribute defines the action to be performed when the form is submitted.
Usually, the form data is sent to a file on the server when the user clicks on the submit button.
<form action="action_page.php">
The Target Attribute
The target attribute specifies where to display the response
that is received after submitting the form.
The target attribute can have one of the following values:
<form action="action_page.php">
<fieldset>
<legend>Personalia:</legend>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"
value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname"
value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
The <datalist> Element
The <datalist> element specifies a list of pre-defined options for an <input> element.
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element,
must refer to the id attribute of the <datalist> element.
<form action="/action_page.php"> HTML <datalist> tag: This tag specifies the predefined
<input list="browsers"> options for an <input> element. This tag also gives the
<datalist id="browsers"> autocomplete feature for the <input> element of HTML.
Like once the user starts typing the input element of <
<option value="Internet Explorer"> datalist> tag, the user will see the pre-defined options
<option value="Firefox"> starting with the letter or word typed by the user. Note
<option value="Chrome"> that to use the <datalist> tag, the id of the tag must be
the same as of the <input> element attribute.
<option value="Opera">
<option value="Safari">
</datalist>
</form>
The <output> Element
The <output> element represents the result of a calculation
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
Input E lements
Here are the different input types you can use in HTML:
•<input type="button">
•<input type="checkbox">
•<input type="color">
•<input type="date">
•<input type="datetime-local">
•<input type="email">
•<input type="file">
•<input type="hidden">
•<input type="image">
•<input type="month">
•<input type="number">
•<input type="password">
•<input type="radio">
•<input type="range">
•<input type="reset">
•<input type="search">
•<input type="submit">
•<input type="tel">
•<input type="text">
•<input type="time">
•<input type="url">
•<input type="week">
Input Type Text
<input type="text"> defines a single-line text input field:
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>
</form>
Input Type Checkbox
<input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
Input Type Date
The <input type="date"> is used for input fields that should contain a date.
Depending on browser support, a date picker can show up in the input field.
<form>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
</form>
Input Type Image
The <input type="image"> defines an image as a submit button.
The path to the image is specified in the src attribute.
<form>
<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
</form>
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="hidden" id="custId" name="custId" value="3487">
<input type="submit" value="Submit">
</form>
Passing variables with data between
pages
• Some time we collect some value from a database and want to
retain the value for the particular user throughout the site as the
user moves between pages. There are different ways for
passing such values of the variables. It can be passed within a
site or even outside the site. We will discuss some of the ways
with their possible uses.
• Passing variable values between pages using session
• Passing variables between pages using cookies
• Passing variables between pages using URL
• Web Forms
• These are some of the way of transferring data variables
between pages. There is no clear rule to use a particular
method to pass the variables between pages. Here are some
standard way of doing with reasons. All variables needs
sanitization before using when they arrive from external
sources.
• There are two ways the browser client can send
information to the web server.
• The GET Method
• The POST Method
• Before the browser sends the information, it encodes it
using a scheme called URL encoding. In this scheme,
name/value pairs are joined with equal signs and
different pairs are separated by the ampersand.
name1=value1&name2=value2&name3=value3
• Spaces are removed and replaced with the + character
and any other nonalphanumeric characters are replaced
with a hexadecimal values. After the information is
encoded it is sent to the server.
PASSING VARIABLES THROUGH The GET Method
http://www.test.com/index.htm?name1=value1&name2=value2
• The GET method is restricted to send upto 1024
characters only.
• Never use GET method if you have password or other
sensitive information to be sent to the server.
• GET can't be used to send binary data, like images or
word documents, to the server.
• The data sent by GET method can be accessed using
QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all
the sent information using GET method.
<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<html>
<body>
</body>
</html>
The POST Method
• The POST method transfers information via HTTP headers. The
information is encoded as described in case of GET method and
put into a header called QUERY_STRING.
• The POST method does not have any restriction on data size to
be sent.
• The POST method can be used to send ASCII as well as binary
data.
• The data sent by POST method goes through HTTP header so
security depends on HTTP protocol. By using Secure HTTP you
can make sure that your information is secure.
• The PHP provides $_POST associative array to access all the sent
information using POST method.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
The $_REQUEST variable
• The PHP $_REQUEST variable contains the contents of
both $_GET, $_POST, and $_COOKIE.
<?php
// Start the session
session_start();
$_SESSION[“Admin"] = “Admin";
$_SESSION[“Password"] = “admin123";
echo "Session variables are set.";
?>
Get PHP Session Variable Values
• session variables are not passed individually to each new
page, instead they are retrieved from the session we open
at the beginning of each page session_start();
• Also notice that all session variable values are stored in the
global $_SESSION variable:
• <?php
session_start();
• // Echo session variables that were set on previous
page
echo "Favorite color is " . $_SESSION[“Admin"]
. ".<br>";
echo "Favorite animal is " . $_SESSION[“Password"]
. ".";
?>
• show all the session variable values for a user session is to run
the following code:
• <?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
print_r($_SESSION);
?>
</body>
</html>
Modify a PHP Session Variable
• <?php
session_start();
?>
• <!DOCTYPE html>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION[“Admin"] = “Rahul";
print_r($_SESSION);
?>
</body>
</html>
Destroy a PHP Session
<?php
session_start();
// remove all session variables
session_unset();