Wit Unit Iv Pmfa
Wit Unit Iv Pmfa
Wit Unit Iv Pmfa
UNIT – IV
Creating and Using Forms: Understanding Common Form Issues, GET vs. POST, Validating
form input, Working with multiple forms, and Preventing Multiple Submissions of a form.
XML: Basic XML- Document Type Definition XML Schema DOM and Presenting XML, XML
Parsers and Validation, XSL and XSLT Transformation, News Feed (RSS and ATOM).
XML
Basic XML:
Extensible Markup Language (XML) is a markup language that defines a set of rules for
encoding documents in a format that is both human-readable and machine-readable. The W3C's
XML 1.0 Specification and several other related specifications —all of them free open standards.
The design goals of XML emphasize simplicity, generality, and usability across the Internet.
What is XML?
XML is Extensible
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 1
Web & Internet technologies UNIT-IV XML, Creating & Using forms
XML also makes it easier to expand or upgrade to new operating systems, new applications,
or new browsers, without losing data.
With XML, data can be available to all kinds of "reading machines" like people, computers,
voice machines, news feeds, etc
In many HTML applications, XML is used to store or transport data, while HTML is used
to format and display the same data.
When displaying data in HTML, you should not have to edit the HTML file when the data
changes. With XML, the data can be stored in separate XML files
XML Example
XML documents create a hierarchical structure looks like a tree so it is known as XML
Tree that starts at "the root" and branches to "the leaves".
<student>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 2
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<name>Rahul</name>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
</student> Output:
The first line is the XML declaration. It defines the XML version (1.0) and the encoding
used (ISO-8859-1 = Latin-1/West European character set).
The next line describes the root element of the document (like saying: "this document is a
student"):
<student>
The next 4 lines describe 4 child elements of the root (name, rollno, yearsem, branch).And
finally the last line defines the end of the root element.
</student>
XML documents must contain a root element. This element is "the parent" of all other
elements. The elements in an XML document form a document tree. The tree starts at the root and
branches to the lowest level of the tree. All elements can have sub elements (child elements).
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 3
Web & Internet technologies UNIT-IV XML, Creating & Using forms
The terms parent, child, and sibling are used to describe the relationships between elements.
Parent elements have children. Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).
<RAM>4GB</RAM>
<price>27500</price>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 4
Web & Internet technologies UNIT-IV XML, Creating & Using forms
</laptop>
<laptop>
<brand>Lenovo</brand>
<HDD>500GB</HDD>
<RAM>8GB</RAM>
<price>23000</price>
</laptop>
</laptops>
</catalogue>
The root element in the example is <catalogue>. All elements in the document are
contained within < catalogue >. <books> & <laptops> elements are siblings to each other and
children of <catalogue>. <book> is child of <books> and it has 3 children <title>, <author> &
<price>. The <laptop> element has 4 children: <brand>,< HDD>, <RAM> and <price>.
Output:
XML Syntax:
XML documents must contain one root element that is the parent of all other elements.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 5
Web & Internet technologies UNIT-IV XML, Creating & Using forms
The XML prolog is optional. If it exists, it must come first in the document. XML
documents can contain international characters, like Norwegian øæå or French êèé. To avoid
errors, you should specify the encoding used, or save your XML files as UTF-8.
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>. Opening and closing
tags must be written with the same case:
In the example above, "Properly nested" simply means that since the <i> element is opened
inside the <b> element, it must be closed inside the <b> element.
XML elements can have attributes in name/value pairs just like in HTML. In XML, the
attribute values must always be quoted:
<book category="Programming">
Comments in XML
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 6
Web & Internet technologies UNIT-IV XML, Creating & Using forms
XML does not truncate multiple white-spaces (HTML truncates multiple white-spaces to
one single white-space):
XML Element:
An XML element is everything from (including) the element's start tag to (including) the element's
end tag.
<price>700</price>
• text
• attributes
• other elements
• or a mix of the above
<title>, <author> and <price> have text content because they contain text (like 700).
<catalogue> and <books> have element contents, because they contain elements.<book> has an
attribute (category="children").
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 7
Web & Internet technologies UNIT-IV XML, Creating & Using forms
• Element names cannot start with the letters xml (or XML, or Xml, etc)
Element names can contain letters, digits, hyphens, underscores, and periods
Element names cannot contain spaces.
<student>
<name>Rahul</name>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
</student>
<student>
<name>Rahul</name>
<fathername>Rohan</fathername>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
<address>Kadapa</address>
</student>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 8
Web & Internet technologies UNIT-IV XML, Creating & Using forms
No. The application should still be able to find the <name>, <rollno>,<yearsem> and <branch>
elements in the XML document and produce the same output.
XML Attributes
XML elements can have attributes, just like HTML. Attributes are designed to contain data related
to a specific element.
Attribute values must always be quoted. Either single or double quotes can be used. For a
person's gender, the <person> element can be written like this:
If the attribute value itself contains double quotes you can use single quotes, like in this
example:
In the first example gender is an attribute. In the second example, gender is an element.
Both examples provide the same information. There are no rules about when to use attributes or
when to use elements in XML.
Sometimes ID references are assigned to elements. These IDs can be used to identify XML
elements in much the same way as the id attribute in HTML. This example demonstrates this:
<laptops>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 9
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<laptop id=”501”>
<brand>Acer</brand> <HDD>1TB</HDD>
<RAM>4GB</RAM>
<price>27500</price>
</laptop>
<laptop id=”502”>
<brand>Lenovo</brand>
<HDD>500GB</HDD>
<RAM>8GB</RAM>
<price>23000</price>
</laptop>
</laptops>
The id attributes above are for identifying the different laptops. It is not a part of the laptop
itself. Metadata (data about data) should be stored as attributes, and the data itself should be stored
as elements.
A DTD is a Document Type Definition. A DTD defines the structure and the legal elements and
attributes of an XML document.
• With a DTD, independent groups of people can agree on a standard DTD for interchanging
data.
• An application can use a DTD to verify that XML data is valid.
//Ex: studentidtd.xml
<?xml version="1.0"?>
<!DOCTYPE student [
<!ELEMENT student (name,rollno,yearsem,branch)>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 10
Web & Internet technologies UNIT-IV XML, Creating & Using forms
]>
<student>
<name>Rohan</name>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
</student> Output:
• !DOCTYPE student defines that the root element of this document is student
• !ELEMENT student defines that the student element must contain four elements:
"name,rollno,yearsem,branch"
• !ELEMENT name defines the name element to be of type "#PCDATA"
• !ELEMENT rollno defines the rollno element to be of type "#PCDATA"
• !ELEMENT yearsem defines the yearsem element to be of type "#PCDATA"
• !ELEMENT branch defines the branch element to be of type "#PCDATA"
<?xml version="1.0"?>
<student>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 11
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<name>Rohan</name>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
</student>
//std.dtd
From DTD point of view, all XML documents are made up by the following building
blocks:
• Elements
• Attributes
• Entities
• PCDATA
• CDATA
Elements
Elements are the main building blocks of both XML and HTML documents.
Examples of HTML elements are "body" and "table". Examples of XML elements could
be "student" and "book". Elements can contain text, other elements, or be empty. Examples of
empty HTML elements are "hr", "br" and "img".
Attributes
Attributes provide extra information about elements. Attributes are always placed inside
the opening tag of an element. Attributes always come in name/value pairs. The following "img"
element has additional information about a source file:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 12
Web & Internet technologies UNIT-IV XML, Creating & Using forms
The name of the element is "img". The name of the attribute is "src". The value of the
attribute is "computer.gif". Since the element itself is empty it is closed by a " /".
Entities
Some characters have a special meaning in XML, like the less than sign (<) that defines the
start of an XML tag. The HTML entity: " ". This "no-breaking-space" entity is used in
HTML to insert an extra space in a document. Entities are expanded when a document is parsed
by an XML parser.
> >
& &
" "
' '
PCDATA
PCDATA means parsed character data. Character data is the text found between the start
tag and the end tag of an XML element.
PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser
for entities and markup.
Tags inside the text will be treated as markup and entities will be expanded. However,
parsed character data should not contain any &, <, or > characters; these need to be represented by
the & < and > entities, respectively.
CDATA
CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be
treated as markup and entities will not be expanded.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 13
Web & Internet technologies UNIT-IV XML, Creating & Using forms
XML Schema:
An XML Schema describes the structure of an XML document, just like a DTD. An XML
document with correct syntax is called "Well Formed". An XML document validated against an
XML Schema is both "Well Formed" and "Valid".
XML Schema is an XML-based alternative to DTD. The XML Schema language is also referred
to as XML Schema Definition (XSD).
//studentschema.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="rollno" type="xs:string"/>
<xs:element name="yearsem" type="xs:string"/>
<xs:element name="branch" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
//stdntschma.xml
<?xml version="1.0"?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentschema.xsd">
<name>Rohan</name>
<rollno>17KH1A0589</rollno>
<yearsem>III-II</yearsem>
<branch>CSE</branch>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 14
Web & Internet technologies UNIT-IV XML, Creating & Using forms
</student> Output:
With XML Schema, your XML files can carry a description of its own format. With XML
Schema, independent groups of people can agree on a standard for interchanging data. With XML
Schema, you can verify data.
One of the greatest strengths of XML Schemas is the support for data types:
Another great strength about XML Schemas is that they are written in XML:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 15
Web & Internet technologies UNIT-IV XML, Creating & Using forms
• You can use your XML editor to edit your Schema files
• You can use your XML parser to parse your Schema files
• You can manipulate your Schemas with the XML DOM
• You can transform your Schemas with XSLT
<?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
The <schema> element may contain some attributes. A schema declaration often looks something like
this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
...
</xs:schema>
DTD vs XSD
There are many differences between DTD (Document Type Definition) and XSD (XML
Schema Definition). In short, DTD provides less control on XML structure whereas XSD (XML
schema) provides more control.
DTD XSD
DTD stands for Document Type Definition. XSD stands for XML Schema Definition.
DTDs are derived from SGML syntax. XSDs are written in XML.
DTD doesn't support datatypes. XSD supports datatypes for elements and
attributes.
DTD doesn't support namespace. XSD supports namespace.
DTD doesn't define order for child elements. XSD defines order for child elements.
DTD is not extensible. XSD is extensible.
DTD is not simple to learn. XSD is simple to learn because you don't need
to learn new language.
DTD provides less control on XML structure. XSD provides more control on XML
structure.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 16
Web & Internet technologies UNIT-IV XML, Creating & Using forms
XML – Namespaces:
Namespace Declaration
A Namespace is declared using reserved attributes. Such an attribute name must either be
xmlns or begin with xmlns: shown as below −
Syntax
Example:
Namespace affects only a limited area in the document. An element containing the
declaration and all of its descendants are in the scope of the Namespace. Following is a simple
example of XML Namespace –
XML parsers can handle documents in any way that their developers choose. There are two
models commonly used for parsers i.e., SAX and DOM. SAX parsers are used when dealing with
streams of data. This type of parsers is usually used with java. SAX-based parsers run quickly.
DOM is and application program interface (API) for XML documents. The DOM API specifies
the logical structure of XML documents and the ways in which they can be accessed and
manipulated. The DOM API is just a specification. DOM-complaint applications include all of the
functionality needed to handle XML documents. They can build static documents, navigate and
search through them, add new elements, delete elements, and modify the content of existing
elements. The views XML document as trees. The DOM exposes the whole of the document to
applications. It is also scriptable so applications can manipulate the individual nodes.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 17
Web & Internet technologies UNIT-IV XML, Creating & Using forms
The HTML DOM defines a standard way for accessing and manipulating HTML
documents. It presents an HTML document as a tree-structure.
The XML DOM defines a standard way for accessing and manipulating XML documents.
It presents an XML document as a tree-structure.
Presenting XML
XML documents are presented using Extensible Stylesheet which expresses stylesheets. XSL
stylesheet are not the same as HTML cascading stylesheets. They create a style for a specific XML
element, with XSL a template is created. XSL basically transforms one data structure to another
i.e., XML to HTML.
XML Parsers:
All major browsers have a built-in XML parser to access and manipulate XML. The XML
DOM (Document Object Model) defines the properties and methods for accessing and editing
XML. However, before an XML document can be accessed, it must be loaded into an XML DOM
object. All modern browsers have a built-in XML parser that can convert text into an XML DOM
object.
An XML parser is a software library or package that provides interfaces for client
applications to work with an XML document. The XML Parser is designed to read the XML and
create a way for programs to use XML. XML parser validates the document and check that the
document is well formatted.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 18
Web & Internet technologies UNIT-IV XML, Creating & Using forms
1. DOM
2. SAX
Advantages
• It supports both read and write operations and the API is very simple to use.
Disadvantages
• It is memory inefficient. (consumes more memory because the whole XML document
needs to loaded into memory).
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 19
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Disadvantages
• It is event-based so its API is less intuitive.
• Clients never know the full information because the data is broken into pieces.
DOM stands for Document Object Model SAX stands for Simple API for XML
DOM is useful when reading small to medium SAX is used when big files need to b parsed
size XML files
//Domparser.html
<html>
<head>
<title>XML DOM Parser</title>
</head>
<body>
<p id="pg"></p>
<script>
var data,parser,xmldoc;
data="<book>"+"<title>Java The Complete Reference</title>"+"<author>Herbert
Schildt</author>"+"<year>2005</year>"+"</book>";
parser = new DOMParser();
xmldoc=parser.parseFromString(data,"text/xml");
document.getElementById("pg").innerHTML=xmldoc.getElementsByTagName("title")[0].childNodes[0
].nodeValue;
</script>
</body>
</html>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 20
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Output:
XML Validation:
A well formed XML document can be validated against DTD or Schema. A well-formed XML
document is an XML document with correct syntax. It is very necessary to know about valid XML
document before knowing XML validation.
A Valid XML Document is a document which must be well formed (Satisfy all the basic syntax
conditions). It should be behave according to predefined DTD or XML schema
XML DTD
A DTD defines the legal elements of an XML document. In simple words we can say that
a DTD defines the document structure with a list of legal elements and attributes. XML schema is
a XML based alternative to DTD. Actually DTD and XML schema both are used to form a well
formed XML document. We should avoid errors in XML documents because they will stop the
XML programs.
XML schema
It is defined as an XML language. Uses namespaces to allow for reuses of existing
definitions. It supports a large number of built in data types and definition of derived data types
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 21
Web & Internet technologies UNIT-IV XML, Creating & Using forms
XSL stands for Extensible Stylesheet Language , is a styling language for XML. XSLT stands
for XSL Transformations. XSLT are used to transform XML documents into some other formats like text
or HTML etc.( Like transforming XML into HTML docs). In simple, XSLT is a language for transforming
XML documents.
HTML uses predefined tags. The meaning of, and how to display each tag is well understood. CSS is used
to add styles to HTML elements.
XML does not use predefined tags, and therefore the meaning of each tag is not well understood.
A <table> element could indicate an HTML table, a piece of furniture, or something else - and browsers
do not know how to display it. So, XSL describes how the XML elements should be displayed.
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or
<xsl:transform>. <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used.
Sytnax :
or
<xsl:template> Element
An XSL style sheet consists of one or more set of rules that are called templates. A template contains rules
to apply when a specified node is matched. The <xsl:template> element is used to build templates. The
match attribute is used to associate a template with an XML element.
<xsl:value-of> Element
The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> element can
be used to extract the value of an XML element and add it to the output stream of the transformation. The
select attribute contains an XPath expression. An XPath expression works like navigating a file system; a
forward slash (/) selects subdirectories.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 22
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<xsl:for-each> Element
The <xsl:for-each> element allows you to do looping in XSLT. The XSL <xsl:for-each> element can be
used to select every XML element of a specified node-set.
//books.xml
<books>
<book>
<author>Herbert Schildt</author>
<price>2300</price>
</book>
<book>
<title>Data Structures</title>
<author>Debasis Samanta</author>
<price>1200</price>
</book>
<book>
<title>C Programming</title>
<price>1250</price>
</book>
</books>
//booksxsl.xsl
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 23
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<xsl:template match="books">
<html>
<body>
<h2>List of Books</h2>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
</tr>
<xsl:for-each select="book">
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="price"/></td></tr>
</xsl:for-each></table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 24
Web & Internet technologies UNIT-IV XML, Creating & Using forms
An Atom feed is very similar to an RSS feed in that it is a lightweight XML format allowing
for easy syndication of web content. In fact, most RSS readers and news aggregators will be able
to read Atom feeds just fine, as it is becoming a widely-used alternative to RSS feeds.
What’s a “feed”?
• RSS feed and ATOM feed are small text files that provide information about content on
websites.
• When content is updated, the feed text file is also updated, either manually or
programatically.
• Applications called “readers” or “aggregators” can then check these small text files and
notify someone when new content is available.
• If you already have an RSS feed, creating an Atom feed is extremely easy.
• With RSS it is possible to distribute up-to-date web content from one web site to thousands
of other web sites around the world.
• RSS allows fast browsing for news and updates.
• RSS stands for Really Simple Syndication
//Ex: rssdemo.xml
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 25
Web & Internet technologies UNIT-IV XML, Creating & Using forms
What is ATOM?
• Atom is the name of an XML-based Web content and metadata syndication format, and an
application-level protocol for publishing and editing Web resources belonging to
periodically updated websites.
• All Atom feeds must be well-formed XML documents, and are identified with the
application/atom+xml media type.
<link href="http://svck.edu.in/"/>
<updated>2020-04-03T18:30:02</updated>
<author>
<name>SVCK</name>
</author>
<entry>
<title>SVCK CSE Department Updates</title>
<link href="http://svck.edu.in/CSE.HTML"/>
<updated>2020-04-03T20:30:02</updated>
<summary>CSE Department Data</summary>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 26
Web & Internet technologies UNIT-IV XML, Creating & Using forms
</entry>
</feed>
Output:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 27
Web & Internet technologies UNIT-IV XML, Creating & Using forms
When dealing with forms, we should remember that we are limited to certain variety of fields that
can be applied to a form. These fields are non-negotiable and work in the way they were created
to work. The below tables shows some of the form elements available.
Element Description
TEXT INPUT A simple text box
PASSWORD INPUT A text box that hides the characters inputted
HIDDEN INPUT A field that does not show on the form but can contain data
SELECT A drop-down box with options
LIST A select box that can have multiple options selected
CHECKBOX A box that can be checked
RADIO A radio button that can act as a choice
TEXTAREA A larger box that can contain paragraph-style entries
FILE An element that allows you to browse your computer for a file
SUBMIT A button that will submit the form
RESET A button that will reset the form to its original state
As part of this topic , we need to understand the common issues in below areas.
GET Vs POST :
When dealing with forms, you must specify the way that the information entered into the
form is transmitted to its destination (method=""). The two ways available to a web developer are
GET and POST.
When sending data using the GET method, all fields are appended to the Uniform Resource
Locator (URL) of the browser and sent along with the address as data. With the POST method,
values are sent as standard input.
Sending data using the GET method means that fields are generally capped at 150
characters, which is certainly not the most effective means of passing information. It is also not a
secure means of passing data, because many people know how to send information to a script using
an address bar.
Sending data using the POST method is quite a bit more secure (because the method cannot
be altered by appending information to the address bar) and can contain as much information as
you choose to send. Therefore, whenever possible, use the POST method for sending information
and then adjust your script to handle it.
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 28
Web & Internet technologies UNIT-IV XML, Creating & Using forms
PHP 5’s current methods for dealing with GET and POST variables are the $_GET and
$_POST superglobals, respectively. By using these two superglobals, you can designate exactly
where the information should be coming from and subsequently handle the data in the way you
want.
//getform.php
<html>
<head>
<title>PHP GET FORM</title>
</head>
<body>
<form action="getdemo.php" method="get">
Student Name : <input type="text" name="sname"><br><br>
Student Roll No:<input type="text" name="srn"><br><br>
Student Branch :<input type="text" name="sbranch"><br><br>
Student Year :<input type="text" name="syear"><br><br>
<input type="submit" name="submit">
</form>
<?php
?>
</body>
</html>
//getdemo.php
<html>
<head>
<title>PHP GET DEMO</title>
</head>
<body>
<?php
if($_GET)
{
$name=$_GET["sname"];
$rollno=$_GET["srn"];
$branch=$_GET["sbranch"];
$year=$_GET["syear"];
echo "$name bearing roll number $rollno belongs to $year $branch";
}
?>
</body>
</html>
Output:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 29
Web & Internet technologies UNIT-IV XML, Creating & Using forms
//postform.php
<html>
<head>
<title>PHP POST FORM</title>
</head>
<body>
<form action="postdemo.php" method="post">
Student Name : <input type="text" name="sname"><br><br>
Student Roll No:<input type="text" name="srn"><br><br>
Student Branch :<input type="text" name="sbranch"><br><br>
Student Year :<input type="text" name="syear"><br><br>
<input type="submit" name="submit">
</form>
<?php
?>
</body>
</html>
//postdemo.php
<html>
<head>
<title>PHP POST DEMO</title>
</head>
<body>
<?php
if($_POST)
{
$name=$_POST["sname"];
$rollno=$_POST["srn"];
$branch=$_POST["sbranch"];
$year=$_POST["syear"];
echo "$name bearing roll number $rollno belongs to $year $branch";
}
?>
</body>
</html>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 30
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Output:
In this day and age of constant attacks on websites, one of the biggest issues is attacking
forms directly. To ensure a suitable submission of form data, validation is key. You have many
ways to validate a form and many form elements to consider. Generally, you need to determine
what qualities you want a piece of data to adhere to and then ensure that the submitted data comes
in the correct form. If the data comes in a format that is not to your liking, you must be ready to
take care of this. The following example shows a few examples of form validation using PHP.
Validation is a way to catch mistakes when they happen (or even better, to prevent them from
happening at all).
Client-side validation: These are the checks that happen in the browser, before a form is
submitted. The goal here is to make life easier for the people filling out the form.
Server-side validation: These are the checks that happen after a form is sent back to the web
server. At this point, it is up to your server-side code to review the details and make sure everything
is proper before continuing. No matter what the browser does, server-side validation is essential.
The following example shows a few examples of form validation using PHP.
//validation.php
<html>
<head>
<title>PHP Form Validation</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
User Name : <input type="text" name="uname">
Password : <input type="password" name="pwd">
<input type="submit" name="submit">
</form>
<?php
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 31
Web & Internet technologies UNIT-IV XML, Creating & Using forms
if($_POST)
{
$name=$_POST["uname"];
$pswd=$_POST["pwd"];
if($name==""||$pswd=="")
{
echo "<font color='red'>Username or Password should not be empty</font>";
}
else if(strlen($name)<6)
{
echo "<font color='red'>Username should not contain less than 6 characters</font>";
}
else if(strlen($pswd)<6)
{
echo "<font color='red'>Password should not contain less than 6 characters</font>";
}
if($name!="" && $pswd!="" && strlen($name)>=6 && strlen($pswd)>=6)
{
echo " Welcome User $name<br><br>";
echo " Your password is $pswd";
}
}
?>
</body>
</html>
Output:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 32
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Sometimes you will need to collect values from more than one page. Most developers do
this for the sake of clarity. By providing forms on more than one page, you can separate blocks of
information and thus create a flexible experience for the user. The problem, therefore, is how to
GET values from each page onto the next page and finally to the processing script.
Being the great developer that you are, you can solve this problem and use the hidden input form
type. When each page loads, you only load the values from the previous pages into hidden form
elements and submit them.
//pag1.php
<html>
<head>
<title>Personal Information</title>
</head>
<body>
<form action="pag2.php" method="post">
First Name : <input type="text" name="fname" required><br><br>
Last Name : <input type="text" name="lname"><br><br>
Father Name : <input type="text" name="faname"><br><br>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 33
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Gender:
<input type="radio" name="gen" value="male" checked>Male
<input type="radio" name="gen" value="female">Female<br><br>
Email id : <input type="email" name="email"><br><br>
Mobile Number : <select name="mbn">
<option value="+91">+91</option>
<option value="+92" selected>+92</option>
<option value="+93">+93</option>
</select>
<input type="number" name="nmb"><br><br>
Area of interest :
<input type="checkbox" name="aoi" value="Networking" checked>Networking
<input type="checkbox" name="aoi" value="Data Mining">Data Mining
<input type="checkbox" name="aoi" value="Cloud Computing">Cloud Computing<br><br>
Photo : <input type="file" name="pic"><br><br>
<input type="Reset" value="CLEAR">
<input type="submit" value="NEXT">
</form>
</body>
<?php
?>
</html>
//pag2.php
<html>
<head>
<title>Contact Information</title>
</head>
<body>
<form action="pag3.php" method="post">
D.No : <input type="text" name="dno"><br><br>
Street Name : <input type="text" name="stname"><br><br>
Address Line1 : <input type="text" name="adr1"><br><br>
Address Line2 : <input type="text" name="adr2"><br><br>
Town / Village : <input type="text" name="twn"><br><br>
District : <select name="dst">
<option>YSR Kadapa</option>
<option>Chittoor</option>
<option>Anantapur</option>
<option>Kurnool</option>
</select> <br><br>
State : <select name="st">
<option>Andhra Pradesh</option>
<option>Telangana</option>
</select><br><br>
<input type="Reset" value="CLEAR">
<input type="submit" value="NEXT">
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 34
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<html>
<head>
<title>Educational Information</title>
</head>
<body>
<form action="pag4.php" method="post">
SSC Roll.No : <input type="text" name="sscrn"><br><br>
SSC Year of Passing<select name="sscyop">
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2017</option>
<option>2018</option>
<option>2019</option>
<option>2020</option>
</select><br><br>
SSC Percentage : <input type="text" name="sscpc"><br><br>
Intermediate Roll.No : <input type="text" name="irn"><br><br>
Intermediate Year of Passing<select name="iyop">
<option>2005</option>
<option>2006</option>
<option>2007</option>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 35
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2017</option>
<option>2018</option>
<option>2019</option>
<option>2020</option>
</select><br><br>
Intermediate Percentage : <input type="text" name="ipc"><br><br>
UG Roll.No : <input type="text" name="urn"><br><br>
UG Year of Passing<select name="uyop">
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
<option>2014</option>
<option>2015</option>
<option>2016</option>
<option>2017</option>
<option>2018</option>
<option>2019</option>
<option>2020</option>
</select><br><br>
UG Percentage : <input type="text" name="upc"><br><br>
<input type="Reset" value="CLEAR">
<input type="submit" value="SUBMIT">
<input type="hidden" name="fname" value="<?php echo $_POST['fname']?>"><br>
<input type="hidden" name="lname" value="<?php echo $_POST['lname']?>"><br>
<input type="hidden" name="faname" value="<?php echo $_POST['faname']?>"><br>
<input type="hidden" name="gen" value="<?php echo $_POST['gen']?>"><br>
<input type="hidden" name="email" value="<?php echo $_POST['email']?>"><br>
<input type="hidden" name="mbn" value="<?php echo $_POST['mbn']?>"><br>
<input type="hidden" name="nmb" value="<?php echo $_POST['nmb']?>"><br>
<input type="hidden" name="aoi" value="<?php echo $_POST['aoi']?>"><br>
<input type="hidden" name="pic" value="<?php echo $_POST['pic']?>"><br>
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 36
Web & Internet technologies UNIT-IV XML, Creating & Using forms
<html>
<head>
<title>Candidate Information</title>
</head>
<body>
<?php
echo "Personal Information <br><br>";
echo "-----------------------------<br><br>";
echo "First Name :", $_POST['fname'],"<br><br>";
echo "Last Name :",$_POST['lname'],"<br><br>";
echo "Father Name : ",$_POST['faname'],"<br><br>";
echo "Gender : ",$_POST['gen'],"<br><br> ";
echo "Email : ",$_POST['email'],"<br><br> ";
echo "Mobile Number : ",$_POST['mbn'],$_POST['nmb'],"<br><br> ";
echo "Area of Interest : ",$_POST['aoi'],"<br><br> ";
echo "Photo : ",$_POST['pic'],"<br><br> ";
echo "-----------------------------<br><br>";
echo "Contact Information <br><br>";
echo "-----------------------------<br><br>";
echo "Door No:",$_POST['dno'],"<br><br>";
echo "Street Name : ",$_POST['stname'],"<br><br>";
echo "Address Line 1 : ",$_POST['adr1'],"<br><br>";
echo "Address Line 2 : ",$_POST['adr2'],"<br><br>";
echo "Town/village: ",$_POST['twn'],"<br><br>";
echo "District : ",$_POST['dst'],"<br><br>";
echo "Street Name : ",$_POST['st'],"<br><br>";
echo "-----------------------------<br><br>";
echo "Educational Information <br><br>";
echo "-----------------------------<br><br>";
echo "SSC Roll.No : ",$_POST['sscrn'],"<br><br>";
echo "SSC Year of Passing : ",$_POST['sscyop'],"<br><br>";
echo "SSC Percentage : ",$_POST['sscpc'],"<br><br>";
echo "Intermediate Roll.No : ",$_POST['irn'],"<br><br>";
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 37
Web & Internet technologies UNIT-IV XML, Creating & Using forms
pag3.php pag4.php
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 38
Web & Internet technologies UNIT-IV XML, Creating & Using forms
Of particular danger are credit card number submittals. If a user continually hits the submit button on a credit
card submittal form, their card may be charged multiple times if the developer has not taken the time to validate against
such an eventuality. You can deal with multiple submittal validation in essentially two ways.
• Server side refers to a script located on the server that is receiving the data.
When we browse some popular websites and submits a form we don’t know whether it is processed or not and
we click submit button again and again hence same request goes to server more than one time.
This results in unpredictable behaviour which only complicates user experience. Avoiding multiple submission
of HTML form is common requirement in Web Applications using Servlet, JSP and any other web technology. One
of the simple solutions is to disable submit button inside HTML and JavaScript instead on server side.
//testsub.php
<html>
<head>
<title>Test Sub</title>
</head>
<body>
<form action="" method="post">
User Name : <input type="text" name="uname"><br><br>
Password : <input type="password" name="pwd"><br><br>
<input type="submit" name="SUBMIT" value="SubmitWAIT" onclick="this.value='Submitting
..';this.disabled='disabled'; this.form.submit();" />
<input type="submit" name="SUBMIT" value="SubmitDISABLE" onclick="this.disabled='disabled'" />
</form>
<?php
?>
</body>
</html>
Output:
Prepared by P.M. Fayaz Ahmed, Asst. Prof, CSE, SVCE, Kadapa Page 39