Web Development Practical
Web Development Practical
DEVELOPMENT
Practical note
UNIT-II - HTML
What is HTML
HTML is an acronym which stands for Hyper Text Markup Language which is used for
creating web pages and web applications. Let's see what is meant by Hypertext Markup
Language, and Web page.
Hyper Text:HyperText simply means "Text within Text." A text has a link within it, is a
hypertext. Whenever you click on a link which brings you to a new webpage, you have
clicked on a hypertext. HyperText is a way to link two or more web pages (HTML
documents) with each other.
Markup language: A markup language is a computer language that is used to apply layout
and formatting conventions to a text document. Markup language makes text more
interactive and dynamic. It can turn text into images, tables, links, etc.
Web Page: A web page is a document which is commonly written in HTML and translated
by a web browser. A web page can be identified by entering an URL. A Web page can be of
the static or dynamic type. With the help of HTML only, we can create static web pages.
Hence, HTML is a markup language which is used for creating attractive web pages with the
help of styling, and which looks in a nice format on a web browser. An HTML document is
made of many HTML tags and each HTML tag contains different content.
1. <!DOCTYPE>
2. <html>
3. <head>
4. <title>Web page title</title>
5. </head>
6. <body>
7. <h1>Write Your First Heading</h1>
8. <p>Write Your First Paragraph.</p>
9. </body>
10. </html>
<html > :This tag informs the browser that it is an HTML document. Text between html tag
describes the web document. It is a container for all other elements of HTML except
<!DOCTYPE>
<title>: As its name suggested, it is used to add title of that HTML page which appears at the
top of the browser window. It must be placed inside the head tag and should close
immediately. (Optional)
<body>: Text between body tag describes the body content of the page that is visible to the
end user. This tag contains the main content of the HTML document.
<h1> : Text between <h1> tag describes the first level heading of the webpage.
<p>: Text between <p> tag describes the paragraph of the webpage.
Tim Berners-Lee is known as the father of HTML. The first available description of HTML
was a document called "HTML Tags" proposed by Tim in late 1991. The latest version of
HTML is HTML5, which we will learn later in this tutorial.
HTML Versions
Since the time HTML was invented there are lots of HTML versions in market, the brief
introduction about the HTML version is given below:
HTML 1.0: The first version of HTML was 1.0, which was the barebones version of HTML
language, and it was released in1991.
HTML 2.0: This was the next version which was released in 1995, and it was standard
language version for website design. HTML 2.0 was able to support extra features such as
form-based file upload, form elements such as text box, option button, etc.
HTML 3.2: HTML 3.2 version was published by W3C in early 1997. This version was
capable of creating tables and providing support for extra options for form elements. It can
also support a web page with complex mathematical equations. It became an official standard
for any browser till January 1997. Today it is practically supported by most of the browsers.
HTML 4.01: HTML 4.01 version was released on December 1999, and it is a very stable
version of HTML language. This version is the current official standard, and it provides
added support for stylesheets (CSS) and scripting ability for various multimedia elements.
Features of HTML
1) It is a very easy and simple language. It can be easily understood and modified.
2) It is very easy to make an effective presentation with HTML because it has a lot of
formatting tags.
3) It is a markup language, so it provides a flexible way to design web pages along with the
text.
4) It facilitates programmers to add a link on the web pages (by html anchor tag), so it
enhances the interest of browsing of the user.
6) It facilitates the programmer to add Graphics, Videos, and Sound to the web pages which
makes it more attractive and interactive.
7) HTML is a case-insensitive language, which means we can use tags either in lower-case or
upper-case.
To run the HTML page, you need to open the file location, where you have saved the file and
then either double-click on file or click on open with option
To open Sublime Text editor go to Start screen type Sublime Text Open it. To open a
new page press CTRL+N.
To save your page in Sublime Text press Ctrl+S or go to File option save, to save a file use
extension .htm or .html. We recommend to save the file first then write the code because
after saving the page sublime text editor will give you suggestions to write code.
To execute or open this page in Web browser just right click by mouse on sublime text page
and click on Open in Browser.
• Tags: An HTML tag surrounds the content and apply meaning to it. It is written
between < and > brackets.
• Attribute: An attribute in HTML provides extra information about the element, and it
is applied within the start tag. An HTML attribute contains two fields: name & value.
Syntax
1. <tag name attribute_name= " attr_value"> content </ tag name>
Output:
When a web browser reads an HTML document, browser reads it from top to bottom and left
to right. HTML tags are used to create HTML documents and render their properties. Each
HTML tags have different properties.
An HTML file must have some essential tags so that web browser can differentiate between a
simple text and HTML text. You can use as many tags you want as per your code
requirement.
Syntax
<tag> content </tag>
<i>Italic Tag</i>
<hr> Tag: hr stands for Horizontal Rule. This tag is used to put a line across the webpage.
HTML Attribute
• HTML attributes are special words which provide additional information about the elements
or attributes are the modifier of the HTML element.
• Each element or tag can have attributes, which defines the behaviour of that element.
• Attributes should always be applied with start tag.
• The Attribute should always be applied with its name and value pair.
• The Attributes name and values are case sensitive, and it is recommended by W3C that it
should be written in Lowercase only.
• You can add multiple attributes in one HTML element, but need to give space between two
attributes.
Syntax
1. <element attribute_name="value">content</element>
Example
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <h1> This is Style attribute</h1>
7. <p style="height: 50px; color: blue">It will add style property in element</p>
8. <p style="color: red">It will change the color of content</p>
9. </body>
10. </html>
Output:
1. <p style="height: 50px; color: blue">It will add style property in element</p>
In the above statement, we have used paragraph tags in which we have applied style
attribute. This attribute is used for applying CSS property on any HTML element. It provides
height to paragraph element of 50px and turns it colour to blue.
In the above statement we have again used style attribute in paragraph tag, which turns its
colour red.
Note: There are some commonly used attributes are given below, and the complete list and
explanation of all attributes are given in HTML attributes List.
Example
With <h1> tag:
1. <p title="This is paragraph tag">Move the cursor over the heading and paragraph, and
you will see a description as a tooltip</p>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6.
7. <h1 title="This is heading tag">Example of title attribute</h1>
8. <p title="This is paragraph tag">Move the cursor over the heading and paragraph, and you
will see a description as a tooltip</p>
9.
10. </body>
11. </html>
Output:
Example
With link address:
Example
1. <img src="whitepeacock.jpg" height="400" width="600">
Note: The above example also have height and width attribute, which define the height and width of
image on web page.
Output:
IN HTML5, you can also omit use of quotes around attribute values.
HTML Elements
An HTML file is made of elements. These elements are responsible for creating web pages
and define content in that webpage. An element in HTML usually consist of a start tag <tag
name>, close tag </tag name> and content inserted between them. Technically, an element
is a collection of start tag, attributes, end tag, content between them.
17 | MonTech Heart of Experience# Vo1
Note: Some elements does not have end tag and content, these elements are termed as empty elements
or self-closing element or void elements.
Such as:
Example
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>WebPage</title>
5. </head>
6. <body>
7. <h1>This is my first web page</h1>
8. <h2> How it looks?</h2>
9. <p>It looks Nice!!!!!</p>
10. </body>
11. </html>
All the content written between body elements are visible on web page.
Void element: All the elements in HTML do not require to have start tag and end tag, some
elements does not have content and end tag such elements are known as Void elements or
empty elements. These elements are also called as unpaired tag.
Some Void elements are <br> (represents a line break) ,<hr>(represents a horizontal
line), etc.
• Block-level element
• Inline element
Block-level element:
• These are the elements, which structure main part of web page, by dividing a page into
coherent blocks.
• A block-level element always start with new line and takes the full width of web page, from
left to right.
• These elements can contain block-level as well as inline elements.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <div style="background-color: lightblue">This is first div</div>
7. <div style="background-color: lightgreen">This is second div</div>
8. <p style="background-color: pink">This is a block level element</p>
9. </body>
10. </html>
Output:
We have used style attribute which is used to styling the HTML content, and the background
color are showing that it's a block level element.
Inline elements:
• Inline elements are those elements, which differentiate the part of a given text and provide it a
particular function.
• These elements does not start with new line and take width as per requirement.
• The Inline elements are mostly used with other elements.
<a>, <abbr>, <acronym>, <b>, <bdo>, <big>, <br>, <button>, <cite>, <code>, <dfn>,
<em>, <i>, <img>, <input>, <kbd>, <label>, <map>, <object>, <q>, <samp>, <script>,
<select>, <small>, <span>, <strong>, <sub>, <sup>, <textarea>, <time>, <tt>, <var>.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <a href="https://www.javatpoint.com/html-tutorial">Click on link</a>
7. <span style="background-color: lightblue">this is inline element</span>
8. <p>This will take width of text only</p>
9. </body>
10. </html>
Output:
HTML Formatting
HTML Formatting is a process of formatting text for better look and feel. HTML provides
us ability to format text without using CSS. There are many formatting tags in HTML. These
tags are used to make text bold, italicized, or underlined. There are almost 14 options
available that how text appears in HTML and XHTML.
• Physical tag: These tags are used to provide the visual appearance to the text.
• Logical tag: These tags are used to add some logical or semantic value to the text.
NOTE: There are some physical and logical tags which may give same visual appearance, but they
will be different in semantics.
Here, we are going to learn 14 HTML formatting tags. Following is the list of HTML
formatting text.
Element
Description name
<b> This is a physical tag, which is used to bold the text written between it.
<strong> This is a logical tag, which tells the browser that the text is important.
<tt> This tag is used to appear a text in teletype. (not supported in HTML5)
<big> This tag is used to increase the font size by one conventional unit.
<small> This tag is used to decrease the font size by one unit from base font size.
1) Bold Text
HTML<b> and <strong> formatting elements
The HTML <b> element is a physical tag which display text in bold font, without any logical
importance. If you write anything within <b>............</b> element, is shown in bold letters.
Output:
The HTML <strong> tag is a logical tag, which displays the content in bold font and informs
the browser about its logical importance. If you write anything between
<strong>???????.</strong>, is shown important text.
Output:
Example
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>formatting elements</title>
5. </head>
6. <body>
7. <h1>Explanation of formatting element</h1>
8. <p><strong>This is an important content</strong>, and this is normal content</p>
9. </body>
10. </html>
The HTML <i> element is physical element, which display the enclosed content in italic
font, without any added importance. If you write anything within <i>............</i> element, is
shown in italic letters.
Output:
The HTML <em> tag is a logical element, which will display the enclosed content in italic
font, with added semantics importance.
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>formatting elements</title>
5. </head>
6. <body>
7. <h1>Explanation of italic formatting element</h1>
8. <p><em>This is an important content</em>, which displayed in italic font.</p>
9. </body>
10. </html>
4) Underlined Text
If you write anything within <u>.........</u> element, is shown in underlined text.
Output:
5) Strike Text
Anything written within <strike>.......................</strike> element is displayed with
strikethrough. It is a thin line which cross the statement.
Output:
6) Monospaced Font
If you want that each letter has the same width then you should write the content within
<tt>.............</tt> element.
Note: We know that most of the fonts are known as variable-width fonts because different
letters have different width. (for example: 'w' is wider than 'i'). Monospaced Font provides
similar space among every letter.
7) Superscript Text
If you put the content within <sup>..............</sup> element, is shown in superscript; means
it is displayed half a character's height above the other characters.
Output:
8) Subscript Text
If you put the content within <sub>..............</sub> element, is shown in subscript ; means it
is displayed half a character's height below the other characters.
HTML Heading
A HTML heading or HTML h tag can be defined as a title or a subtitle which you want to
display on the webpage. When you place the text within the heading tags <h1>.........</h1>, it
is displayed on the browser in the bold format and size of the text depends on the number of
heading.
There are six different HTML headings which are defined with the <h1> to <h6> tags, from
highest level h1 (main heading) to the least level h6 (least important heading).
h1 is the largest heading tag and h6 is the smallest one. So h1 is used for most important
heading and h6 is used for least important.
Headings in HTML helps the search engine to understand and index the structure of
web page.
Heading no. 1
Heading no. 2
Heading no. 3
Heading no. 4
Heading no. 5
Heading no. 6
Heading elements (h1....h6) should be used for headings only. They should not be used just to make
text bold or big.
HTML headings can also be used with nested elements. Following are different codes to
display the way to use heading elements.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Heading elements</title>
5. </head>
6. <body>
7. <h1>This is main heading of page. </h1>
8. <p>h1 is the most important heading, which is used to display the keyword of
page </p>
9. <h2>This is first sub-heading</h2>
10. <p>h2 describes the first sub heading of page. </p>
11. <h3>This is Second sub-heading</h3>
12. <p>h3 describes the second sub heading of page.</p>
13. <p>We can use h1 to h6 tag to use the different subheading with their
paragraphs if
14. required.
15. </p>
16. </body>
17. </html>
HTML Paragraph
HTML paragraph or HTML p tag is used to define a paragraph in a webpage. Let's take a
simple example to see how it work. It is a notable point that a browser itself add an empty
line before and after a paragraph. An HTML <p> tag indicates starting of new paragraph.
Note: If we are using various <p> tags in one HTML file then browser automatically adds a single
blank line between the two paragraphs.
Output:
1. <p>
2. I am
3. going to provide
4. you a tutorial on HTML
5. and hope that it will
6. be very beneficial for you.
7. </p>
8. <p>
9. Look, I put here a lot
10. of spaces but I know, Browser will ignore it.
11. </p>
12. <p>
13. You cannot determine the display of HTML</p>
14. <p>because resized windows may create different result.
15. </p>
Output:
I am going to provide you a tutorial on HTML and hope that it will be very beneficial for
you.
Look, I put here a lot of spaces but I know, Browser will ignore it.
As you can see, all the extra lines and unnecessary spaces are removed by the browser.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
An HTML <hr> tag is used to apply a horizontal line between two statements or two
paragraphs. Following is the example which is showing use of <hr> tag with paragraph.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. </head>
5. <body>
6. <h2> Example to show a horizontal line with paragraphs</h2>
7. <p> An HTML hr tag draw a horizontal line and separate two paragraphs with that
line.<hr> it will start a new paragraph.
8. </p>
9. </body>
10. </html>
Output:
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title></title>
Note:
• The target attribute can only use with href attribute in anchor tag.
• If we will not use target attribute then link will open in same page.
HTML Image
HTML img tag is used to display image on the web page. HTML img tag is an empty tag
that contains attributes only, closing tags are not used in HTML image element.
1) src
It is a necessary attribute that describes the source or path of the image. It instructs the
browser where to look for the image on the server.
2) alt
The alt attribute defines an alternate text for the image, if it can't be displayed. The value of
the alt attribute describe the image in words. The alt attribute is considered good for SEO
prospective.
3) width
It is an optional attribute which is used to specify the width to display the image. It is not
recommended now. You should apply CSS in place of width attribute.
4) height
It h3 the height of the image. The HTML height attribute also supports iframe, image and
object elements. It is not recommended now. You should apply CSS in place of height
attribute.
Note: Always try to insert the image with height and width, else it may flicker while displaying on
webpage.
Output:
Note: If src URL will be incorrect or misspell then it will not display your image on web page, so try
to put correct URL.
Example:
1. <a href="https://www.javatpoint.com/what-isrobotics"><img
src="robot.jpg" height="100" width="100"></a>
Output:
We can create a table to display data in tabular form, using <table> element, with the help of
<tr> ,<td>, and <th> elements.
In Each table, table row is defined by <tr> tag, table header is defined by <th>, and table data
is defined by <td> tags.
HTML tables are used to manage the layout of the page e.g. header section, navigation bar,
body content, footer section etc. But it is recommended to use div tag over table to manage
the layout of the page .
1. <table>
2. <tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>
3. <tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr>
4. <tr><td>James</td><td>William</td><td>80</td></tr>
5. <tr><td>Swati</td><td>Sironi</td><td>82</td></tr>
6. <tr><td>Chetna</td><td>Singh</td><td>72</td></tr>
7. </table>
Output:
HTML Lists
HTML Lists are used to specify lists of information. All lists may contain one or more list
elements. There are three different types of HTML lists:
Note: We can create a list inside another list, which will be termed as nested List.
1. <ol>
2. <li>Aries</li>
3. <li>Bingo</li>
4. <li>Leo</li>
5. <li>Oracle</li>
6. </ol>
Output:
1. Aries
2. Bingo
Click here for full details of HTML ordered list. HTML Ordered List
1. <ul>
2. <li>Aries</li>
3. <li>Bingo</li>
4. <li>Leo</li>
5. <li>Oracle</li>
6. </ul>
Output:
• Aries
• Bingo
• Leo
• Oracle
The definition list is very appropriate when you want to present glossary, list of terms or
other name-value list.
1. <dl>
2. <dt>Aries</dt>
3. <dd>-One of the 12 horoscope sign.</dd>
4. <dt>Bingo</dt>
5. <dd>-One of my evening snacks</dd>
Output:
Aries
-One of the 12 horoscope sign.
Bingo
-One of my evening snacks
Leo
-It is also an one of the 12 horoscope sign.
Oracle
-It is a multinational technology corporation.
Click here for full details of HTML description list. HTML Description List
Code:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Nested list</title>
5. </head>
6. <body>
7. <p>List of Indian States with thier capital</p>
8. <ol>
9. <li>Delhi
10. <ul>
11. <li>NewDelhi</li>
12. </ul>
13. </li>
14. <li>Haryana
15. <ul>
To represent different ordered lists, there are 5 types of attributes in <ol> tag.
Type Description
Type "1" This is the default type. In this type, the list items are numbered with numbers.
Type "I" In this type, the list items are numbered with upper case roman numbers.
Type "i" In this type, the list items are numbered with lower case roman numbers.
Type "A" In this type, the list items are numbered with upper case letters.
Type "a" In this type, the list items are numbered with lower case letters.
1. <ol>
2. <li>HTML</li>
3. <li>Java</li>
4. <li>JavaScript</li>
5. <li>SQL</li>
6. </ol>
Output:
1. HTML
2. Java
3. JavaScript
4. SQL
1. <ol type="I">
2. <li>HTML</li>
3. <li>Java</li>
4. <li>JavaScript</li>
5. <li>SQL</li>
6. </ol>
Output:
I. HTML
II. Java
III. JavaScript
IV. SQL
ol type="i"
Let's see the example to display list in roman number lowercase.
1. <ol type="i">
2. <li>HTML</li>
3. <li>Java</li>
4. <li>JavaScript</li>
5. <li>SQL</li>
6. </ol>
Output:
i. HTML
ii. Java
iii. JavaScript iv. SQL
ol type="A"
Let's see the example to display list in alphabet uppercase.
1. <ol type="A">
2. <li>HTML</li>
3. <li>Java</li>
4. <li>JavaScript</li>
Output:
A. HTML
B. Java
C. JavaScript
D. SQL
ol type="a"
Let's see the example to display list in alphabet lowercase.
1. <ol type="a">
2. <li>HTML</li>
3. <li>Java</li>
4. <li>JavaScript</li>
5. <li>SQL</li>
6. </ol>
Output:
a. HTML
b. Java
c. JavaScript
d. SQL
start attribute
The start attribute is used with ol tag to specify from where to start the list items.
<ol type="1" start="5"> : It will show numeric values starting with "5".
<ol type="A" start="5"> : It will show capital alphabets starting with "E".
<ol type="a" start="5"> : It will show lower case alphabets starting with "e".
<ol type="I" start="5"> : It will show Roman upper case value starting with "V".
<ol type="i" start="5"> : It will show Roman lower case value starting with "v".
Output:
v. HTML
vi. Java
vii. JavaScript
viii. SQL
HTML Form
An HTML form is a section of a document which contains controls such as text fields,
password fields, checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for processing
such as name, email address, password, phone number, etc. .
For example: If a user want to purchase some items on internet, he/she must fill the form
such as shipping address and credit/debit card details so that item can be sent to the given
address.
Tag Description
Tag Description
Note: The <form> element does not itself create a form but it is container to contain all required form
elements, such as <input>, <label>, etc.
Syntax:
1. <form>
2. //Form elements
3. </form>
Example:
1. <body>
2. <form>
3. Enter your name <br>
4. <input type="text" name="username">
5. </form>
6. </body>
Output:
1. <form>
2. First Name: <input type="text" name="firstname"/> <br/>
3. Last Name: <input type="text" name="lastname"/> <br/>
4. </form> Output:
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <form>
8. Enter your address:<br>
9. <textarea rows="2" cols="20"></textarea>
10. </form>
11. </body>
12. </html>
NOTE: It is good to use <label> tag with form, although it is optional but if you will use it, then it will
provide a focus when you tap or click on label tag. It is more worthy with touchscreens.
1. <form>
2. <label for="firstname">First Name: </label> <br/>
3. <input type="text" id="firstname" name="firstname"/> <br/>
4. <label for="lastname">Last Name: </label>
5. <input type="text" id="lastname" name="lastname"/> <br/>
6. </form>
1. <form>
2. <label for="password">Password: </label>
3. <input type="password" id="password" name="password"/> <br/>
4. </form>
Output:
1. <form>
Note: If we will not enter the correct email, it will display error like:
If you use one name for all the radio buttons, only one radio button can be selected at a time.
Using radio buttons for multiple options, you can only choose a single option at a time.
1. <form>
2. <label for="gender">Gender: </label>
3. <input type="radio" id="gender" name="gender" value="male"/>Male
4. <input type="radio" id="gender" name="gender" value="female"/>Female <br/>
Checkbox Control
The checkbox control is used to check multiple options from given checkboxes.
1. <form>
2. Hobby:<br>
3. <input type="checkbox" id="cricket" name="cricket" value="cricket"/>
4. <label for="cricket">Cricket</label> <br>
5. <input type="checkbox" id="football" name="football" value="football"/>
6. <label for="football">Football</label> <br>
7. <input type="checkbox" id="hockey" name="hockey" value="hockey"/>
8. <label for="hockey">Hockey</label>
9. </form>
Note: These are similar to radio button except it can choose multiple options at a time and radio
button can select one button at a time, and its display.
Output:
Syntax:
The value attribute can be anything which we write on button on web page.
Example:
1. <form>
2. <label for="name">Enter name</label><br>
3. <input type="text" id="name" name="name"><br>
4. <label for="pass">Enter Password</label><br>
5. <input type="Password" id="pass" name="pass"><br>
6. <input type="submit" value="submit">
7. </form>
Output:
Example:
1. <form>
2. <fieldset>
3. <legend>User Information:</legend>
4. <label for="name">Enter name</label><br>
5. <input type="text" id="name" name="name"><br>
6. <label for="pass">Enter Password</label><br>
7. <input type="Password" id="pass" name="pass"><br>
8. <input type="submit" value="submit">
9. </fieldset>
10. lt;/form>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <h2>Registration form</h2>
8. <form>
9. <fieldset>
10. <legend>User personal information</legend>
11. <label>Enter your full name</label><br>
12. <input type="text" name="name"><br>
13. <label>Enter your email</label><br>
14. <input type="email" name="email"><br>
15. <label>Enter your password</label><br>
16. <input type="password" name="pass"><br>
17. <label>confirm your password</label><br>
18. <input type="password" name="pass"><br>
19. <br><label>Enter your gender</label><br>
20. <input type="radio" id="gender" name="gender" value="male"/>Male <br>
21. <input type="radio" id="gender" name="gender" value="female"/>Female <br/
>
22. <input type="radio" id="gender" name="gender" value="others"/>others <br/>
23. <br>Enter your Address:<br>
24. <textarea></textarea><br>
25. <input type="submit" value="sign-up">
26. </fieldset>
27. </form>
28. </body>
29. </html>
button Defines a simple push button, which can be programmed to perform a task on an event.
month Defines a control with month and year, without time zone.
week Defines a field to enter the date with week-year, without time zone.
search Defines a single line text field for entering a search string.
1. <input type="text">:
<input> element of type "text" are used to define a single-line input text field.
Example:
1. <form>
2. <label>Enter first name</label><br>
3. <input type="text" name="firstname"><br>
4. <label>Enter last name</label><br>
5. <input type="text" name="lastname"><br>
6. <p><strong>Note:</strong>The default maximum cahracter lenght is 20.</p>
7. </form> Output:
The <input> element of type "password" allow a user to enter the password securely in a
webpage. The entered text in password filed converted into "*" or ".", so that it cannot be
read by another user.
Example:
1. <form>
2. <label>Enter User name</label><br>
3. <input type="text" name="firstname"><br>
4. <label>Enter Password</label><br>
5. <input type="Password" name="password"><br>
6. <br><input type="submit" value="submit">
7. </form>
Output:
The "password"field defines a sinlge line input password field to enter the password
securely.
Enter User name
Enter Password
3. <input type="submit">:
The <input> element of type "submit" defines a submit button to submit the form to the
server when the "click" event occurs.
Example:
1. <form action="https://www.javatpoint.com/html-tutorial">
2. <label>Enter User name</label><br>
3. <input type="text" name="firstname"><br>
4. <label>Enter Password</label><br>
5. <input type="Password" name="password"><br>
6. <br><input type="submit" value="submit">
7. </form> Output:
Enter Password
After clicking on submit button, this will submit the form to server and will redirect the page
to action value.We will learn about "action" attribute in later chapters
4. <input type="reset">:
The <input> type "reset" is also defined as a button but when the user performs a click event,
it by default reset the all inputted values.
Example:
1. <form>
2. <label>User id: </label>
3. <input type="text" name="user-id" value="user">
4. <label>Password: </label>
5. <input type="password" name="pass" value="pass"><br><br>
6. <input type="submit" value="login">
7. <input type="reset" value="Reset">
8. </form> Output:
Try to change the input values of user id and password, then when you click on reset, it will
reset input fields with default values.
5. <input type="radio">:
The <input> type "radio" defines the radio buttons, which allow choosing an option between
a set of related options. At a time only one radio button option can be selected at a time.
Example:
1. <form>
2. <p>Kindly Select your favorite color</p>
3. <input type="radio" name="color" value="red"> Red <br>
4. <input type="radio" name="color" value="blue"> blue <br>
5. <input type="radio" name="color" value="green">green <br>
6. <input type="checkbox">:
The <input> type "checkbox" are displayed as square boxes which can be checked or
unchecked to select the choices from the given options.
Note: The "radio" buttons are similar to checkboxes, but there is an important difference between
both types: radio buttons allow the user to select only one option at a time, whereas checkbox allows
a user to select zero to multiple options at a time. Example:
1. <form>
2. <label>Enter your Name:</label>
3. <input type="text" name="name">
4. <p>Kindly Select your favourite sports</p>
5. <input type="checkbox" name="sport1" value="cricket">Cricket<br>
6. <input type="checkbox" name="sport2" value="tennis">Tennis<br>
7. <input type="checkbox" name="sport3" value="football">Football<br>
8. <input type="checkbox" name="sport4" value="baseball">Baseball<br>
9. <input type="checkbox" name="sport5"
value="badminton">Badminton<br><br>
Output:
Registration Form
Enter your Name:
7. <input type="button">:
The <input> type "button" defines a simple push button, which can be programmed to
control a functionally on any event such as, click event.
1. <form>
2. <input type="button" value="Clcik me " onclick="alert('you are learning HTML')">
3. </form>
Output:
Note: In the above example we have used the "alert" of JS, which you will learn in our JS tutorial. It is
used to show a pop window.
8. <input type="file">:
The <input> element with type "file" is used to select one or more files from user device
storage. Once you select the file, and after submission, this file can be uploaded to the server
with the help of JS code and file API.
Example:
1. <form>
2. <label>Select file to upload:</label>
Output:
9. <input type="image">:
The <input> type "image" is used to represent a submit button in the form of image.
Example:
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <h2>Input "image" type.</h2>
5. <p>We can create an image as submit button</p>
6. <form>
7. <label>User id:</label><br>
8. <input type="text" name="name"><br><br>
9. <input type="image" alt="Submit" src="login.png" width="100px">
10. </form>
11.
12. </body>
13. </html>
The <input> type "color" is used to define an input field which contains a colour. It allows a
user to specify the colour by the visual colour interface on a browser.
Note: The "color" type only supports color value in hexadecimal format, and the default value
is #000000 (black). Example:
1. <form>
2. Pick your Favorite color: <br><br>
<input type="color" name="upclick" value="#a52a2a"> Upclick<br><br>
<input type="color" name="downclick" value="#f5f5dc"> Downclick
</form>
Output:
Up-click
Down-click
Note:The default value of "color" type is #000000 (black). It only supports color value in
hexadecimal format.
2. <input type="date">:
The <input> element of type "date" generates an input field, which allows a user to input the
date in a given format. A user can enter the date by text field or by date picker interface.
Example:
1. <form>
2. Select Start and End Date: <br><br>
3. <input type="date" name="Startdate"> Start date:<br><br>
4. <input type="date" name="Enddate"> End date:<br><br>
5. <input type="submit">
6. </form> Output:
Start date:
End date:
3. <input type="datetime-local">:
The <input> element of type "datetime-local" creates input filed which allow a user to select
the date as well as local time in the hour and minute without time zone information.
Example:
1. <form>
2. <label>
Select the meeting schedule: <br><br>
Select date & time: <input type="datetime-local" name="meetingdate"> <br><br>
</label>
6. <input type="submit">
7. </form>
Output:
4. <input type="email">:
The <input> type "email" creates an input filed which allow a user to enter the e-mail
address with pattern validation. The multiple attributes allow a user to enter more than one
email address.
Example:
1. <form>
2. <label><b>Enter your Email-address</b></label>
3. <input type="email" name="email" required>
4. <input type="submit">
5. <p><strong>Note:</strong>User can also enter multiple email addresses
separat ing by comma or whitespace as following: </p>
6. <label><b>Enter multiple Email-addresses</b></label>
7. <input type="email" name="email" multiple>
8. <input type="submit">
9. </form> Output:
Note:User can also enter multiple email addresses separating by comma or whitespace as
following:
The <input> type "month" creates an input field which allows a user to easily enter month
and year in the format of "MM, YYYY" where MM defines month value, and YYYY defines
the year value. New
Example:
1. <form>
2. <label>Enter your Birth Month-year: </label>
3. <input type="month" name="newMonth">
4. <input type="submit">
5. </form> Output:
The action attribute value defines the web page where information proceed. It can be .php,
.jsp, .asp, etc. or any URL where you want to process your form.
Note: If action attribute value is blank then form will be processed to the same page. Example:
64 | P a g e
Output:
User Password
It will redirect to a new page "action.html" when you click on submit button
The method attribute defines the HTTP method which browser used to submit the form. The
possible values of method attribute can be:
post: We can use the post value of method attribute when we want to process the sensitive
data as it does not display the submitted data in URL.
Example:
get: The get value of method attribute is default value while submitting the form. But this is
not secure as it displays data in URL after submitting the form.
Example:
When submitting the data, it will display the entered data in the form of:
1. file:///D:/HTML/action.html?name=JavaTPoint&pass=123
_self: If we use _self as an attribute value, then the response will display in current page only.
Example:
_blank: If we use _blank as an attribute it will load the response in a new page.
Example:
Example:
Note: it can be used with <form> element and <input> element both.
Example:
multipart/form-data: It does not encode any character. It is used when our form contains file-
upload controls.
Example:
text/plain (HTML5): In this encoding type only space are encoded into + symbol and no any
other special character encoded.
Example:
Enter age:
Enter email:
Try to change the form detials with novalidateatttribute and without novalidate attribute
and see the difference.
Note: One should not omit the name attribute as when we submit the form the HTTP request includes
both name-value pair and if name is not available it will not process that input field. Example:
Output:
Enter age:
Enter email:
Note: If you will not use name attribute in any input field, then that input field will not
be submitted, when submit the form.
Click on submit and see the URL where email is not included in HTTP request as we have
not used name attribute in the email input field
Example:
1. <form>
2. <label>Enter your Name</label><br>
3. <input type="text" name="uname" value="Enter Name"><br><br>
4. <label>Enter your Email-address</label><br>
5. <input type="text" name="uname" value="Enter email"><br><br>
6. <label>Enter your password</label><br>
7. <input type="password" name="pass" value=""><br><br>
8. <input type="submit" value="login">
9. </form> Output: Fill the form
Enter your Name
Enter Name
Note: In password input filed the value attribute will always unclear
Example:
1. <form>
2. <label>Enter your Email-address</label><br>
3. <input type="text" name="uname" required><br><br>
4. <label>Enter your password</label><br>
5. <input type="password" name="pass"><br><br>
6. <input type="submit" value="login">
7. </form> Output: Fill the form
Enter your Email-address
If you will try to submit the form without completing email field then it will give an error
pop up.
Example:
1. <form>
2. <label>Enter your Email-address</label><br>
3. <input type="text" name="uname" autofocus><br><br>
4. <label>Enter your password</label><br>
5. <input type="password" name="pass"><br><br>
6. <input type="submit" value="login">
7. </form>
The placeholder attribute can be used with text, password, email, and URL values.
When the user enters the value, the placeholder will be automatically removed.
Example:
1. <form>
2. <label>Enter your name</label><br>
3. <input type="text" name="uname" placeholder="Your name"><br><br>
4. <label>Enter your Email address</label><br>
5. <input type="email" name="email"
placeholder="example@gmail.com"><br>< br>
6. <label>Enter your password</label><br>
7. <input type="password" name="pass" placeholder="your
password"><br><br>
8. <input type="submit" value="login">
9. </form> Output:
The disabled input filed does not receive click events, and these input value will not be sent
to the server when submitting the form.
Example:
Example:
Output:
Account number
CVV
Output:
User Name:
User password:
The email field is outside the form but still it will remain part of the form
User email:
CSS is used to apply the style in the web page which is made up of HTML elements. It
describes the look of the webpage.
CSS provides various style properties such as background color, padding, margin,
bordercolor, and many more, to style a webpage.
Each property in CSS has a name-value pair, and each property is separated by a semicolon
(;).
Note: In this chapter, we have given a small overview of CSS. You will learn everything in depth
about CSS in our CSS tutorial. Example:
In the above example, we have used a style attribute to provide some styling format to our
code.
Output:
• Inline CSS: Define CSS properties using style attribute in the HTML elements.
• Internal or Embedded CSS: Define CSS using <style> tag in <head> section.
• External CSS: Define all CSS property in a separate .css file, and then include the file with
HTML file using tag in section.
Inline CSS:
Inline CSS is used to apply CSS in a single element. It can apply style uniquely in each
element.
To apply inline CSS, you need to use style attribute within HTML element. We can use as
many properties as we want, but each property should be separated by a semicolon (;).
Example:
Output:
Internal CSS:
An Internal stylesheets contains the CSS properties for a webpage in <head> section of
HTML document. To use Internal CSS, we can use class and id attributes.
We can use internal CSS to apply a style for a single HTML page.
Example:
Note: In the above example, we have used a class attribute which you will learn in the next chapter.
External CSS:
An external CSS contains a separate CSS file which only contains style code using the class
name, id name, tag name, etc. We can use this CSS file in any HTML file by including it in
HTML file using <link> tag.
If we have multiple HTML pages for an application and which use similar CSS, then we can
use external CSS.
Example:
CSS file:
body{
background-color:lavender;
text-align: center;
}
h2{
font-style: italic;
size: 30px;
color: #f08080;
} p{
font-size: 20px;
}
.blue{ color:
blue;
} .red{
color: red; }
.green{
color: green;
}
padding padding: 20px; It defines the space between content and the border.
Example:
1. <head>
2. <style>
3. .headings{
4. color: lightgreen;
5. font-family: cursive;
6. background-color: black; }
7. </style>
8. </head>
We have define style for a class name "headings", and we can use this class name with any of
HTML element in which we want to provide such styling. We just need to follow the
following syntax to use it.
Example 1:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .headings{
6. color: lightgreen;
7. font-family: cursive;
8. background-color: black; }
9. </style>
Let's use a class name "Fruit" with CSS to style all elements.
1. <style>
2. .fruit {
3. background-color: orange;
4. color: white;
5. padding: 10px;
6. }
7. </style>
8.
9. <h2 class="fruit">Mango</h2>
10. <p>Mango is king of all fruits.</p> 11.
12. <h2 class="fruit">Orange</h2>
13. <p>Oranges are full of Vitamin C.</p>
15. <h2 class="fruit">Apple</h2>
16. <p>An apple a day, keeps the Doctor away.</p>
Here you can see that we have used the class name "fruit" with (.) to use all its elements.
Note: You can use class attribute on any HTML element. The class name is case-sensitive.
Example:
Let's hide all the elements with class name "fruit" when the user click on the button.
1. <!DOCTYPE html>
Note: You will learn more about JavaScript in our JavaScript tutorial.
Multiple Classes
You can use multiple class names (more than one) with HTML elements. These class names
must be separated by a space.
Example:
Let's style elements with class name "fruit" and also with a class name "center".
1. <!DOCTYPE html>
2. <html>
3. <style>
4. .fruit {
5. background-color: orange;
6. color: white;
7. padding: 10px;
8. }
9.
You can see that the first element <h2> belongs to both the "fruit" class and the "center"
class.
Example:
1. <!DOCTYPE html>
2. <html>
3. <style>
4. .fruit {
5. background-color: orange;
6. color: white;
7. padding: 10px;
8. }
9. </style>
10. <body>
11. <h2>Same Class with Different Tag</h2>
12. <h2 class="fruit">Mango</h2>
13. <p class="fruit">Mango is the king of all fruits.</p>
14. </body>
15. </html>
Note: In the Cascading Style sheet (CSS), we can easily select an element with the specific id by using
the # symbol followed by id.
Note: JavaScript can access an element with the given ID by using the getElementById() method.
Syntax
1. <tag id="value">
Example 1: The following example describes how to use the id attribute in CSS document:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>
5. Example of Id attribute in CSS
6. </title>
7. <style>
8. #Cars {
9. padding: 40px;
10. background-color: lightblue;
11. color: black;
12. text-align: center; 13. }
14.
15. #Bikes
16. {
17. padding: 50px;
18. background-color: lightGreen;
19. text-align: center;
20. }
21. </style>
22. </head>
23. <body>
24. <p> Use CSS to style an element with the id: </p>
25. <h1 id="Cars"> Cars </h1>
26. <h1 id="Bikes"> Bikes </h1>
27. </body>
28. </html>
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title> Date Attribute </title>
5. <script>
6. function viewdate() {
7. var x = document.getElementById("dob").value;
8. document.getElementById("demo").innerHTML = x;
9. </script>
10. </head>
11. <body>
12. Employee Name: <input type="text" placeholder="Your Good name"/>
13. <br>
14. <br>
15. Date of Joining:
16. <input type="date" id="dob">
17. <br>
18. <button onclick="viewdate()"> Submit
19. </button>
20. <br>
Syntax
To create a list box, use the HTML element<select> which contains two attributes Name and
Size. The Name attribute is used to define the name for calling the list box, and size attribute
is used to specify the numerical value that shows the how many options it contains.
Example 1: Consider the below example that creates a simple list box.
1. <!DOCTYPE html>
2. <html>
3. <title>
4. Example of List Box
5. </title>
6. <body>
7. Customer Name: <input type="text" Placeholder="Enter the Customer
Name"/>
8. <br>
9. <br>
10. <select name="Cars" size="5">
11. <option value="Merceders"> Merceders </option>
12. <option value="BMW"> BMW </option>
13. <option value="Jaguar"> Jaguar </option>
14. <option value="Lamborghini"> Lamborghini </option>
15. <option value="Ferrari"> Ferrari </option>
16. <option value="Ford"> Ford </option>
17. </select>
18. </body>
19. </html> Output:
Example 2: Below example uses the multipleattribute for selecting the multiple options in a
list. We can select multiple options from list box by holding the ctrl key.
1. <!DOCTYPE>
2. <html>
3. <head>
4. <style>
5. h1{
6. color:white;
7. background-color:red;
8. padding:5px;
9. }
10. p{
11. color:blue;
12. }
13. </style> 14. </head>
15. <body>
16. <h1>Write Your First CSS Example</h1>
17. <p>This is Paragraph.</p>
18. </body>
19. </html>
Output:
CSS is used along with HTML and JavaScript in most websites to create user interfaces for
web applications and user interfaces for many mobile applications.
Selector: Selector indicates the HTML element you want to style. It could be any tag like
<h1>, <title> etc.
Declaration Block: The declaration block can contain one or more declarations separated by
a semicolon. For the above example, there are two declarations:
1. color: yellow;
2. font-size: 11 px;
Property: A Property is a type of attribute of HTML element. It could be color, border etc.
Value: Values are assigned to CSS properties. In the above example, value "yellow" is
assigned to color property.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <p>This style will be applied on every paragraph.</p>
13. <p id="para1">Me too!</p>
14. <p>And me!</p>
15. </body>
16. </html> Test it Now
Output:
Me too!
And me!
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id
is always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
Let?s take an example with the id "para1".
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. #para1 {
6. text-align: center;
7. color: blue;
8. }
Output:
Hello Javatpoint.com
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. .center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is blue and center-aligned.</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html> Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.center {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1 class="center">This heading is not affected</h1>
13. <p class="center">This paragraph is blue and center-aligned.</p>
14. </body>
15. </html> Test it Now
Output:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. *{
6. color: green;
7. font-size: 20px;
8. }
9. </style>
Output:
This is heading
Me too!
And me!
Grouping selector is used to minimize the code. Commas are used to separate each selector in
grouping.
1. h1 {
2. text-align: center;
3. color: blue;
4. }
5. h2 {
6. text-align: center;
7. color: blue;
8. }
9. p{
10. text-align: center;
11. color: blue;
12. }
As you can see, you need to define CSS properties for all the elements. It can be grouped in
following ways:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1, h2, p {
6. text-align: center;
7. color: blue;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. <h2>Hello Javatpoint.com (In smaller font)</h2>
14. <p>This is a paragraph.</p>
15. </body>
16. </html>
Output:
Hello Javatpoint.com
Hello Javatpoint.com (In smaller font)
This is a paragraph.
1. Inline CSS
2. Internal CSS
3. External CSS
1) Inline CSS
Inline CSS is used to apply CSS on a single line or element.
2) Internal CSS
Internal CSS is used to apply CSS on a single document or page. It can affect all the elements
of the page. It is written inside the style tag within head section of html.
For example:
1. <style>
2. p{color:blue}
3. </style>
3) External CSS
External CSS is used to apply CSS on multiple pages or all pages. Here, we write all the CSS
code in a css file. Its extension must be .css for example style.css.
For example:
1. p{color:blue}
You need to link this style.css file to your html pages like this:
Inline CSS
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document. This method
mitigates some advantages of style sheets so it is advised to use this method sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant tag.
Syntax:
Example:
Output:
Internal CSS
The internal style sheet is used to add a unique style for a single document. It is defined in
<head> section of the HTML page inside the <style> tag.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-color: linen;
7. }
8. h1 {
9. color: red;
10. margin-left: 80px;
11. }
12. </style>
13. </head>
14. <body>
15. <h1>The internal style sheet is applied on this heading.</h1>
External CSS
The external style sheet is generally used when you want to make changes on multiple pages.
It is ideal for this condition because it facilitates you to change the look of the entire web site
by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside the head section.
Example:
1. <head>
2. <link rel="stylesheet" type="text/css" href="mystyle.css">
3. </head>
The external style sheet may be written in any text editor but must be saved with a .css
extension. This file should not contain HTML elements.
File: mystyle.css
1. body {
2. background-color: lightblue;
3. }
4. h1 {
5. color: navy;
6. margin-left: 20px;
7. }
Note: You should not use a space between the property value and the unit. For example: It
should be margin-left:20px not margin-left:20 px.
CSS Comments
CSS comments are generally written to explain your code. It is very helpful for the users who
reads your code so that they can easily understand the code.
Comments are single or multiple lines statement and written within /*............*/ .
1. <!DOCTYPE html>
2. <html>
95 | MonTech Heart of Experience# Vo1
3. <head>
4. <style>
5. p{
6. color: blue;
7. /* This is a single-line comment */
8. text-align: center;
9. }
10. /* This is
11. a multi-line
12. comment */
13. </style>
14. </head>
15. <body>
16. <p>Hello Javatpoint.com</p>
17. <p>This statement is styled with CSS.</p>
18. <p>CSS comments are ignored by the browsers and not shown in the output.</p>
19. </body>
20. </html>
Output:
Hello Javatpoint.com
CSS comments are ignored by the browsers and not shown in the output.
CSS Background
CSS background property is used to define the background effects on element. There are 5
CSS background properties that affects the HTML elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1) CSS background-color
The background-color property is used to specify the background color of the element.
1. <!DOCTYPE html>
Output:
2) CSS background-image
The background-image property is used to set an image as a background of an element. By
default the image covers the entire element. You can set the background image for a page like
this.
1. <!DOCTYPE html>
2. <html>
3. <head> 4. <style>
5. body {
6. background-image: url("paper1.gif");
7. margin-left:100px;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. </body>
14. </html>
Note: The background image should be chosen according to text color. The bad combination
of text and background image may be a cause of poor designed and not readable webpage.
background-repeat: repeat-x;
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-image: url("gradient_bg.png");
7. background-repeat: repeat-x;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. </body>
14. </html> background-repeat: repeat-y;
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-image: url("gradient_bg.png");
7. background-repeat: repeat-y;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. </body>
14. </html>
4) CSS background-attachment
The background-attachment property is used to specify if the background image is fixed or
scroll with the rest of the page in browser window. If you set fixed the background image
then the image will not move during scrolling in the browser. Let?s take an example with
fixed background image.
5) CSS background-position
The background-position property is used to define the initial position of the background
image. By default, the background image is placed on the top-left of the webpage.
1. center
2. top
3. bottom
4. left
5. right
CSS Border
The CSS border is a shorthand property used to set the border on an element.
The CSS border properties are use to specify the style, color and size of the border of an
element. The CSS border properties are given below
• border-style
• border-color
• border-width
• border-radius
1) CSS border-style
The Border style property is used to specify the border type which you want to display on the
web page.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.none {border-style: none;}
6. p.dotted {border-style: dotted;}
Output:
No border.
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
An inset border.
An outset border.
A hidden border.
2) CSS border-width
The border-width property is used to set the border's width. It is set in pixels. You can also
use the one of the three pre-defined values, thin, medium or thick to set the width of the
border.
Note: The border-width property is not used alone. It is always used with other border properties like
"border-style" property to set the border first otherwise it will not work.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.one {
6. border-style: solid;
7. border-width: 5px;
8. }
9. p.two {
10. border-style: solid;
11. border-width: medium;
12. }
13. p.three {
14. border-style: solid;
15. border-width: 1px;
16. }
17. </style>
18. </head>
19. <body>
20. <p class="one">Write your text here.</p>
21. <p class="two">Write your text here.</p>
22. <p class="three">Write your text here.</p>
23. </body>
3) CSS border-color
There are three methods to set the color of the border.
There is also a border color named "transparent". If the border color is not set it is inherited
from the color property of the element.
Note: The border-color property is not used alone. It is always used with other border properties like
"border-style" property to set the border first otherwise it will not work.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.one {
6. border-style: solid;
7. border-color: red;
8. }
9. p.two {
10. border-style: solid;
11. border-color: #98bf21;
12. }
13. </style>
14. </head>
15. <body>
16. <p class="one">This is a solid red border</p>
17. <p class="two">This is a solid green border</p>
18. </body>
19. </html>
This property has two main values that are separate and collapse. When it is set to the value
separate, the distance between the cells can be defined using the border-spacing property.
When the border-collapse is set to the value collapse, then the inset value of border-style
property behaves like groove, and the outset value behaves like ridge.
Syntax
Values
separate: It is the default value that separates the border of the table cell. Using this value,
each cell will display its own border.
collapse: This value is used to collapse the borders into a single border. Using this, two
adjacent table cells will share a border. When this value is applied, the border-spacing
property does not affect. initial: It sets the property to its default value. inherit: It
inherits the property from its parent element.
Now, let's understand this CSS property by using some examples. In the first example, we are
using the separate value of the border-collapse property. In the second example, we are
using the collapse value of the border-collapse property.
With this value, we can use the border-spacing property to set the distance between the
adjacent table cells.
1. <!DOCTYPE html>
2. <html>
3.
4. <head>
5. <title> border-collapse property </title>
6. <style>
7. table{
8. border: 2px solid blue;
9. text-align: center;
10. font-size: 20px;
11. width: 80%;
12. height: 50%;
13. }
14. th{
15. border: 5px solid red;
16. background-color: yellow;
17. }
18. td{
19. border: 5px solid violet;
20. background-color: cyan;
21. }
22. #t1 {
23. border-collapse: separate;
24. }
Test it Now
Output
The border-spacing and border-radius properties cannot be used with this value.
1. <!DOCTYPE html>
2. <html>
3.
4. <head>
Test it Now
Output
It can be defined as one or two values for determining the vertical and horizontal spacing.
• When only one value is specified, then it sets both horizontal and vertical spacing.
• When we use the two-value syntax, then the first one is used to set the horizontal spacing
(i.e., the space between the adjacent columns), and the second value sets the vertical spacing
(i.e., the space between the adjacent rows).
Syntax
Property Values
length: This value sets the distance between the borders of the adjacent table cells in px, cm,
pt, etc. Negative values are not allowed.
Let's understand this CSS property by using some examples. In the first example, we are
using the single value of the border-spacing property, and in the second example, we are
using two values of the border-spacing property.
Example
Here, we are using the single value of the border-spacing property. The border-collapse
property is set to separate, and the value of the border-spacing is set to 45px.
1. <!DOCTYPE html>
Output
Example
Here, we are using two values of the border-spacing property. The border-collapse property
is set to separate, and the value of the border-spacing is set to 20pt 1em. The first value,
i.e., 20pt sets the horizontal spacing, and the value 1em set the vertical spacing.
1. <!DOCTYPE html>
2. <html>
3.
4. <head>
5. <title> border-spacing property </title>
6. <style>
7. table{
8. border: 2px solid blue;
9. text-align: center;
10. font-size: 20px;
11. background-color: lightgreen;
12. }
13. th{
14. border: 5px solid red;
15. background-color: yellow;
16. }
17. td{
18. border: 5px solid violet;
19. background-color: cyan;
20. }
21. #space{
22. border-collapse: separate;
23. border-spacing: 20pt 1em;
24. }
25. </style>
26. </head>
27.
28. <body> 29.
30. <h1> The border-spacing Property </h1>
CSS Display
CSS display is the most important property of CSS which is used to control the layout of the
element. It specifies how the element is displayed.
Every element has a default display value according to its nature. Every element on the
webpage is a rectangular box and the CSS property defines the behavior of that rectangular
box.
1. display: inline;
2. display: inline-block;
3. display: block;
4. display: run-in;
5. display: none;
• <span>
• <a>
• <em>
• <b> etc.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p {
6. display: inline;
7. }
8. </style> 9. </head>
10. <body>
11. <p>Hello Javatpoint.com</p>
12. <p>Java Tutorial.</p>
13. <p>SQL Tutorial.</p>
14. <p>HTML Tutorial.</p>
15. <p>CSS Tutorial.</p>
16. </body>
17. </html>
Hello Javatpoint.com Java Tutorial. SQL Tutorial. HTML Tutorial. CSS Tutorial.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p {
6. display: inline-block;
7. }
8. </style>
9. </head>
10. <body>
11. <p>Hello Javatpoint.com</p>
12. <p>Java Tutorial.</p>
13. <p>SQL Tutorial.</p>
14. <p>HTML Tutorial.</p>
15. <p>CSS Tutorial.</p>
16. </body>
17. </html>
Output:
Hello Javatpoint.com Java Tutorial. SQL Tutorial. HTML Tutorial. CSS Tutorial.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. display: block;
Output:
Hello Javatpoint.com
Java Tutorial.
SQL Tutorial.
HTML Tutorial.
CSS Tutorial.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p {
6. display: run-in;
7. }
8. </style> 9. </head>
10. <body>
11. <p>Hello Javatpoint.com</p>
12. <p>Java Tutorial.</p>
Output:
Hello Javatpoint.com
Java Tutorial.
SQL Tutorial.
HTML Tutorial.
CSS Tutorial.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1.hidden {
6. display: none;
7. }
8. </style>
9. </head>
10. <body>
11. <h1>This heading is visible.</h1>
12. <h1 class="hidden">This is not visible.</h1>
13. <p>You can see that the hidden heading does not contain any space.</p>
14. </body>
15. </html>
Output:
CSS Cursor
It is used to define the type of mouse cursor when the mouse pointer is on the element. It
allows us to specify the cursor type, which will be displayed to the user. When a user hovers
on the link, then by default, the cursor transforms into the hand from a pointer.
The illustration of using the above values of cursor property is given below:
Example
1. <html>
2. <head>
3. </head>
4. <style>
5. body{
6. background-color: lightblue;
7. color:green;
8. text-align: center;
9. font-size: 20px; 10. }
11.
12. </style>
13.
14. <body>
15. <p>Move your mouse over the below words for the cursor change.</p>
16. <div style = "cursor:alias">alias Value</div>
17. <div style = "cursor:auto">auto Value</div>
18. <div style = "cursor:all-scroll">all-scroll value</div>
19. <div style = "cursor:col-resize">col-resize value</div>
20. <div style = "cursor:crosshair">Crosshair</div>
21. <div style = "cursor:default">Default value</div>
CSS Buttons
In HTML, we use the button tag to create a button, but by using CSS properties, we can style
the buttons. Buttons help us to create user interaction and event processing. They are one of
the widely used elements of web pages.
During the form submission, to view or to get some information, we generally use buttons.
background-color
As we have discussed earlier, this property is used for setting the background color of the
button element.
Syntax
Example
1. <!DOCTYPE html>
2. <html> 3.
4. <head>
5. <title>
6. button background Color 7. </title>
8.
9. <style>
10. body{
11. text-align: center;
12. }
13. button {
14. color:lightgoldenrodyellow;
15. font-size: 30px;
16. }
17. .b1 {
18. background-color: red;
19. }
20. .b2 {
21. background-color: blue;
22. }
23. .b3 {
24. background-color: violet;
25. }
26. </style>
27. </head>
28.
29. <body>
30. <h1>The background-color property.</h1>
31. <button class="b1">Red color button</button>
32. <button class="b2">Blue color button</button>
33. <button class="b3">Violet color button</button>
34. </body> 35. </html>
border
It is used to set the border of the button. It is the shorthand property for border-width,
border-color, and border-style.
Syntax
Example
1. <!DOCTYPE html>
2. <html> 3.
4. <head>
5. <title>
6. button background Color 7. </title>
8.
9. <style>
10. body{
11. text-align: center;
12. }
13. button {
14. color:lightgoldenrodyellow;
15. font-size: 30px;
16. }
17. .b1 {
18. background-color: red;
19. border:none;
20. }
21. .b2 {
22. background-color: blue;
23. border:5px brown solid;
24. }
25. .b3 {
26. background-color: yellow;
27. color:black;
28. border:5px red groove;
29. }
30. .b4{
31. background-color:orange;
32. border: 5px red dashed;
33. }
34. .b5{
35. background-color: gray;
36. border: 5px black dotted;
37. }
38. .b6{
39. background-color: lightblue;
40. border:5px blue double;
41. }
42. </style>
border-radius
It is used to make the rounded corners of the button. It sets the border radius of the button.
Syntax
1. element {
2. // border-radius property
3. }
Example
1. <!DOCTYPE html>
2. <html> 3.
4. <head>
5. <title>
6. button background Color 7. </title>
8.
9. <style>
10. body{
11. text-align: center;
12. }
13. button {
14. color:lightgoldenrodyellow;
15. font-size: 30px;
16. }
17. .b1 {
18. background-color: red;
19. border:none;
20. }
21. .b2 {
22. background-color: blue;
23. border:5px brown solid;
box-shadow
As its name implies, it is used to create the shadow of the button box. It is used to add the
shadow to the button. We can also create a shadow during the hover on the button.
Syntax
Example
1. <!DOCTYPE html>
2. <html>
3.
4. <head>
5. <title>
6. button background Color 7. </title>
8.
9. <style>
10. body{
11. text-align: center;
12. }
13. button {
14. color:lightgoldenrodyellow;
15. font-size: 30px;
16. }
17. .b1{
18. background-color: lightblue;
19. border:5px red double;
20. border-radius: 25px;
21. color:black;
22. box-shadow : 0 8px 16px 0 black,
23. 0 6px 20px 0 rgba(0, 0, 0, 0.19);
24. }
25. .b2{
26. background-color: lightblue;
27. border:5px red dotted;
28. color:black;
29. border-radius: 25px;
30. }
31. .b2:hover{
32. box-shadow : 0 8px 16px 0 black,
33. 0 6px 20px 0 rgba(0, 0, 0, 0.19);
34. }
35. </style>
36. </head>
37.
38. <body>
39. <button class="b1">Shadow on button</button>
40. <button class="b2">Box-shadow on hover</button>
41. </body> 42. </html>
Syntax
1. element {
2. // padding style
3. }
Example
1. <!DOCTYPE html>
2. <html> 3.
4. <head>
5. <title> button background Color </title>
8.
9. <style>
10. body{
11. text-align: center;
12. }
13. button {
14. color:lightgoldenrodyellow;
15. font-size: 30px;
16. }
17. .b1 {
18. background-color: red;
19. border:none;
20. padding: 16px;
21. }
22. .b2 {
23. background-color: blue;
24. border:5px brown solid;
25. padding:15px 30px 25px 40px;
26. }
27. .b3 {
28. background-color: yellow;
29. color:black;
30. border:5px red groove;
31. padding-top:30px;
32. }
33. .b4{
34. background-color:orange;
It defines the amount of space above and below inline elements. It allows you to set the
height of a line of independently from the font size.
CSS Margin
CSS Margin property is used to define the space around elements. It is completely transparent
and doesn't have any background color. It clears an area around the element.
Top, bottom, left and right margin can be changed independently using separate properties.
You can also change all properties at once by using shorthand margin property.
margin This property is used to set all the properties in one declaration.
Value Description
length It is used to specify a margin pt, px, cm, etc. its default value is 0px. %
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. background-color: pink;
7. }
8. p.ex {
9. margin-top: 50px;
10. margin-bottom: 50px;
11. margin-right: 100px;
Output:
There are four types to specify the margin property. You can use one of them.
1. <!DOCTYPE html>
2. <html>
3. <head>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. background-color: pink;
7. }
8. p.ex {
9. margin: 50px 100px 150px;
10. }
11. </style>
12. </head>
13. <body>
14. <p>This paragraph is not displayed with specified margin. </p>
15. <p class="ex">This paragraph is displayed with specified margin.</p>
16. </body>
17. </html>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p{
6. background-color: pink;
7. }
8. p.ex {
9. margin: 50px 100px;
10. }
11. </style>
12. </head>
13. <body>
14. <p>This paragraph is not displayed with specified margin. </p>
15. <p class="ex">This paragraph is displayed with specified margin.</p>
16. </body>
17. </html>
UNIT-III – JavaScript
Javascript basics
Although, JavaScript has no connectivity with Java programming language. The name was
suggested and provided in the times when Java was gaining popularity in the market. In
addition to web browsers, databases such as CouchDB and MongoDB uses JavaScript as
their scripting and query language.
Features of JavaScript
There are following features of JavaScript:
1. All popular web browsers support JavaScript as they provide built-in execution environments.
2. JavaScript follows the syntax and structure of the C programming language. Thus, it is a
structured programming language.
3. JavaScript is a weakly typed language, where certain types are implicitly cast (depending on
the operation).
4. JavaScript is an object-oriented programming language that uses prototypes rather than using
classes for inheritance.
5. It is a light-weighted and interpreted language.
6. It is a case-sensitive language.
7. JavaScript is supportable in several operating systems including, Windows, macOS, etc.
8. It provides good control to the users over the web browsers.
History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In the year 1994,
Netscape was founded by Marc Andreessen. He realized that the web needed to become
more dynamic. Thus, a 'glue language' was believed to be provided to HTML to make web
designing easy for designers and part-time programmers. Consequently, in 1995, the
company recruited Brendan Eich intending to implement and embed Scheme programming
language to the browser. But, before Brendan could start, the company merged with Sun
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
• Client-side validation,
• Dynamic drop-down menus,
• Displaying date and time,
• Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box
and prompt dialog box), Displaying clocks etc.
JavaScript Example
1. <script>
2. document.write("Hello JavaScript by JavaScript");
3. </script>
JavaScript Example
Javascript example is easy to code. JavaScript provides 3 places to put the JavaScript code:
within body tag, within head tag and external JavaScript file.
1. <script type="text/javascript">
2. document.write("JavaScript is a simple language for javatpoint learners");
3. </script>
Test it Now
The text/javascript is the content type that provides information to the browser about the
data.
1. <script type="text/javascript">
2. alert("Hello Javatpoint");
3. </script>
In this example, we are creating a function msg(). To create function in JavaScript, you need
to write function with function_name as given below.
To call function, you need to work on event. Here we are using onclick event to call msg()
function.
1. <html>
2. <head>
3. <script type="text/javascript">
4. function msg(){
5. alert("Hello Javatpoint");
6. }
7. </script>
8. </head>
9. <body>
10. <p>Welcome to JavaScript</p>
11. <form>
12. <input type="button" value="click" onclick="msg()"/>
13. </form>
14. </body>
15. </html>
It provides code re usability because single JavaScript file can be used in several html pages.
An external JavaScript file must be saved by .js extension. It is recommended to embed all
JavaScript files into a single file. It increases the speed of the webpage.
Let's create an external JavaScript file that prints Hello Javatpoint in a alert dialog box.
message.js
1. function msg(){
2. alert("Hello Javatpoint");
3. }
Let's include the JavaScript file into html page. It calls the JavaScript function on button
click.
index.html
1. <html>
2. <head>
3. <script type="text/javascript" src="message.js"></script>
4. </head>
5. <body>
6. <p>Welcome to JavaScript</p>
7. <form>
8. <input type="button" value="click" onclick="msg()"/>
9. </form>
10. </body>
11. </html>
1. The stealer may download the coder's code using the url of the js file.
2. If two js files are dependent on one another, then a failure in one file may affect the
execution of the other dependent file.
3. The web browser needs to make an additional http request to get the js code.
4. A tiny to a large change in the js code may cause unexpected results in all its
dependent files.
5. We need to check each file that depends on the commonly created external javascript
file.
6. If it is a few lines of code, then better to implement the internal javascript code.
JavaScript Comment
1. JavaScript comments
2. Advantage of javaScript comments
3. Single-line and Multi-line comments
The JavaScript comments are meaningful way to deliver message. It is used to add
information about the code, warnings or suggestions so that end user can easily interpret the
code.
The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.
1. To make code easy to understand It can be used to elaborate the code so that end user can
easily understand the code.
2. To avoid the unnecessary code It can also be used to avoid the code being executed.
Sometimes, we add the code to perform some action. But after sometime, there may be need
to disable the code. In such case, it is better to use comments.
1. Single-line Comment
2. Multi-line Comment
Let’s see the example of single-line comment i.e. added before the statement.
1. <script>
2. // It is single line comment
3. document.write("hello javascript");
4. </script>
Test it Now
Let’s see the example of single-line comment i.e. added after the statement.
1. <script>
2. var a=10;
3. var b=20;
4. var c=a+b;//It adds values of a and b variable
5. document.write(c);//prints sum of 10 and 20
6. </script>
It is represented by forward slash with asterisk then asterisk with forward slash. For example:
1. <script>
2. /* It is multi line comment.
3. It will not be displayed */
4. document.write("example of javascript multiline comment"); 5. </script>
javaScript Variable
A JavaScript variable is simply a name of storage location. There are two types of variables
in JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
1. <script>
2. var x = 10;
3. var y = 20;
4. var z=x+y;
5. document.write(z);
6. </script>
1. <script>
2. function abc(){
3. var x=10;//local variable
4. }
5. </script>
Or,
1. <script>
2. var data=200;//gloabal variable
3. function a(){
4. document.writeln(data);
5. }
6. function b(){
7. document.writeln(data);
8. }
9. a();//calling JavaScript function
10. b();
11. </script>
1. <script>
2. var value=50;//global variable
3. function a(){
4. alert(value);
5. }
6. function b(){
7. alert(value);
8. }
9. </script>
1. window.value=90;
Now it can be declared inside any function and can be accessed from any function. For
example:
1. function m(){
2. window.value=100;//declaring global variable by window object
3. }
4. function n(){
5. alert(window.value);//accessing global variable from other function
6. }
1. var value=50;
2. function a(){
3. alert(window.value);//accessing global variable
4. }
Objects are the basic run-time bodies in an object-oriented framework. They may represent a
place, a person, an account, a table of data, or anything that the program needs to handle.
Objects can also represent user-defined data such as vectors, time, and lists.
Classes
We know that objects hold the data and the functions to manipulate the data. However, the
two can be bound together in a user-defined data type with the help of classes. Any number
of objects can be created in a class. Each object is associated with the data of type class. A
class is therefore a collection of objects of similar types.
For example, consider the class “Fruits”. We can create multiple objects for this class -
Fruit Mango;
Encapsulation
Encapsulation is the wrapping up/binding of data and function into a single unit called class.
Data encapsulation is the most prominent feature of a class wherein the data is not accessible
to the outside world, and only those functions wrapped inside the class can access it. These
functions serve as the interface between the object’s data and the program.
Inheritance
The phenomenon where objects of one class acquire the properties of objects of another class
is called Inheritance. It supports the concept of hierarchical classification. Consider the object
“car” that falls in the class “Vehicles” and “Light Weight Vehicles”.
In OOP, the concept of inheritance ensures reusability. This means that additional features
can be added to an existing class without modifying it. This is made possible by deriving a
new class from the existing one.
Science",
this.name = name;
this.age = age;
this.studies = studies;
this.price = price;
car is ${this.name}.`)
console.log(car1.name); console.log(car2.maker);
Encapsulation in JavaScript
Encapsulation includes wrapping the property and the function within a single unit.
add_Address(add){
this.add = add;
Address: ${this.add}`);
person1.add_Address('Bangalore');
person1.getDetails();
Memory management in any programming language involves three important phases, termed
as memory life-cycle −
In JavaScript, we do not need to care about allocating memory for simple variables. We can
directly assign values to some variables and it will allocate necessary memory on its own.
Syntax
var variable1 = <value>
var variable2 = <value>
Example
Source Code
<head>
<title>HTML Console</title>
</head>
<body>
<h3> Output Console </h3>
<p> Output:</p>
<div id="output">
</div>
<div id="opError" style="color : #ff0000">
</div>
<script> var
content =''
var error =''
varopDiv=document.querySelector('#output')
From the above example, it is clear that numbers and strings are single values, and allocation
is also simple. But for objects and arrays, JavaScript can also easily allocate the memory
based on their values.
Like variable value assignment, we can also create some memory blocks by calling some
functions. For example, when a function returns a separate object it will automatically assign
a new memory block to the system.
Syntax
Memory_reference = <function call which returns any value>
Examples
The following example uses a function that works on an HTML document. So this program
will run on a browser or HTML editor.
Source Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
</head>
<body> <script>
var e =document.createElement('div');
Example
Initially allocating memory for a variable, then reading the value from it. Writing a new value
and again reading from it.
Source Code
<!DOCTYPE html>
<html>
<head>
<title>HTML Console</title>
</head>
<body>
<h3> Output Console </h3>
<p> Output:</p>
<div id="output">
</div>
<div id="opError" style="color : #ff0000">
</div>
<script> var
content =''
var error =''
opDiv=document.querySelector('#output')
varopErrDiv=document.querySelector('#opError')
Syntax delete
<variable_name>
The variable must be allocated beforehand, otherwise, it will raise an error while trying to
delete that variable. Let us see one example to understand this concept clearly.
Example
Source Code
<!DOCTYPE html>
<html>
<head>
<title>HTML Console</title>
</head>
<body>
<h3> Output Console </h3>
<p> Output:</p>
<div id="output">
</div>
<div id="opError" style="color : #ff0000">
</div>
<script> var
content =''
var error =''
varopDiv=document.querySelector('#output')
varopErrDiv=document.querySelector('#opError')
Unit IV – PHP
PHP Introduction
What is PHP?
• PHP stands for PHP: Hypertext Preprocessor
• PHP is a server-side scripting language, like ASP
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
• PHP is an open source software
• PHP is free to download and use
What is MySQL?
• MySQL is a database server
• MySQL is ideal for both small and large applications
• MySQL supports standard SQL
• MySQL compiles on a number of platforms
• MySQL is free to download and use
PHP + MySQL
• PHP combined with MySQL are cross-platform (you can develop in Windows and
serve on a
Unix platform)
Why PHP?
• PHP runs on different platforms (Windows, Linux, Unix, etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• PHP is FREE to download from the official PHP resource: www.php.net
Where to Start?
To get access to a web server with PHP support, you can:
• Install Apache (or IIS) on your own server, install PHP, and MySQL Or find a
web hosting plan with PHP and MySQL support
PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.
<?php
?>
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting
code.
Below, we have an example of a simple PHP script which sends the text "Hello World" to the
browser:
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.
<html>
<body>
<?php
//This is a comment
/*
This is
a
comment
block */
?>
</body>
</html>
PHP Variables
A variable is used to store information.
Variables in PHP
Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;
New PHP programmers often forget the $ sign at the beginning of the variable. In that case it
will not work.
Let's try creating a variable containing a string, and a variable containing a number:
Hello World
Now, lets try to use some different functions and operators to manipulate the string.
<?php
$txt1="Hello World!"; $txt2="What a nice day!"; echo
$txt1 . " " . $txt2; ?>
If we look at the code above you see that we used the concatenation operator two times. This
is because we had to insert a third string (a space character), to separate the two strings.
<?php
echo strlen("Hello world!");
?>
The output of the code above will be:
12
<?php
echo strpos("Hello world!","world"); ?>
The position of the string "world" in the example above is 6. The reason that it is 6 (and not
7), is that the first character position in the string is 0, and not 1.
PHP Operators
Operators are used to operate on values.
PHP Operators
This section lists the different operators used in PHP.
Assignment Operators
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
Comparison Operators
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
The if Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax
<html>
<body>
</body>
</html>
Notice that there is no ..else.. in this syntax. The code is executed only if the specified
condition is true.
Syntax
Example
The following example will output "Have a nice weekend!" if the current day is Friday,
otherwise it will output "Have a nice day!":
<html>
<body>
<?php
$d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!";
?>
</body>
</html>
< html>
< body>
< ?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
< /body>
< /html>
Syntax
Example
The following example will output "Have a nice weekend!" if the current day is Friday, and
"Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice
day!":
<html>
<body>
<?php
$d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have
a nice Sunday!"; else echo "Have a nice day!";
</body>
</html>
PHP Loops
Often when you write code, you want the same block of code to run over and over again in a
row.
Instead of adding several almost equal lines in a script we can use loops to perform a task like
this.
In PHP, we have the following looping statements:
Syntax
while (condition)
{ code to be executed; }
Example
The example below defines a loop that starts with i=1. The loop will continue to run as long
as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
</body>
</html>
Output:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Syntax
do
{ code to be
executed;
} while
(condition);
Example
The example below defines a loop that starts with i=1. It will then increment i with 1, and
write some output. Then the condition is checked, and the loop will continue to run as long as
i is less than, or equal to 5:
<?php
$i=1;
do
{
$i++; echo "The number is " . $i .
"<br />";
} while ($i<=5);
?>
</body>
</html>
Output:
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The for loop and the foreach loop will be explained in the next chapter.
Syntax
for (init; condition; increment)
{ code to be
executed;
Parameters:
• init: Mostly used to set a counter (but can be any code to be executed once at the
beginning of the loop)
• condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop
continues. If it evaluates to FALSE, the loop ends.
• increment: Mostly used to increment a counter (but can be any code to be executed
at the end of the loop)
Note: Each of the parameters above can be empty, or have multiple expressions (separated
by commas).
Example
The example below defines a loop that starts with i=1. The loop will continue to run as long
as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
<html>
<body>
The number is 1
The number is 2
The number si 3
The number is 4
The number is 5
Example
The following example demonstrates a loop that will print the values of the given array:
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{ echo $value . "<br
/>";
}
?>
</body>
</html>
What is MySQL?
MySQL is a database.
The data in MySQL is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
Databases are useful when storing information categorically. A company may have a database
with the following tables: "Employees", "Products", "Customers" and "Orders".
Queries
A query is a question or a request.
With MySQL, we can query a database for specific information and have a recordset returned.
Look at the following query:
The query above selects all the data in the "LastName" column from the "Persons" table, and
will return a recordset like this:
LastNam
e
Hansen
Svendson
Pettersen
Parameter Description
servername Optional. Specifies the server to connect to. Default value is
"localhost:3306"
username Optional. Specifies the username to log in with. Default value is the name of
the user that owns the server process
password Optional. Specifies the password to log in with. Default is ""
Note: There are more available parameters, but the ones listed above are the most
important. Visit our full PHP MySQL Reference for more details.
Example
In the following example we store the connection in a variable ($con) for later use in the
script. The "die" part will be executed if the connection fails:
< ?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die(“*** Connection FAILED***”);
} else {echo “*** Connection Succeeded ***";}
// some code
?>
Closing a Connection
The connection will be closed automatically when the script ends. To close the connection
before, use the mysql_close() function:
// some code
mysql_close($con);
?>
Create a Database
The CREATE DATABASE statement is used to create a database in MySQL.
Syntax
CREATE DATABASE database_name
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example
The following example creates a database called "my_db":
mysql_close($con);
?>
Create a Table
The CREATE TABLE statement is used to create a table in MySQL.
Syntax
CREATE TABLE table_name
(
column_name1 data_type, column_name2 data_type, column_name3 data_type,
....
)
Example
The following example creates a table named "Persons", with three columns. The column
names will be "FirstName", "LastName" and "Age":
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
Important: A database must be selected before a table can be created. The database is
selected with the mysql_select_db() function.
Note: When you create a database field of type varchar, you must specify the maximum
length of the field, e.g. varchar(15).
The data type specifies what type of data the column can hold. For a complete reference of all
the data types available in MySQL, go to our complete Data Types reference.
Example
$sql = "CREATE TABLE Persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their
values:
The second form specifies both the column names and the values to be inserted:
Example
In the previous chapter we created a table named "Persons", with three columns; "Firstname",
"Lastname" and "Age". We will use the same table in this example. The following example
adds two new records to the "Persons" table:
<?php
$con =
mysql_connect("localhost","root",""); if
(!$con)
{ die('Could not connect: ' .
mysql_error());
}
mysql_select_db("my_db", $con);
mysql_close($con);
?>
When a user clicks the submit button in the HTML form in the example above, the form data
is sent to "insert.php".
The "insert.php" file connects to a database, and retrieves the values from the form with the
PHP $_POST variables.
Then, the mysql_query() function executes the INSERT INTO statement, and a new record
will be added to the "Persons" table.
Here is the "insert.php" page:
< ?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Syntax
SELECT column_name(s)
FROM table_name
Example
The following example selects all the data stored in the "Persons" table (The * character
selects all the data in the table):
<?php
$con =
mysql_connect("localhost","root",""); if
(!$con)
{ die('Could not connect: ' .
mysql_error());
}
mysql_select_db("my_db", $con);
while($row = mysql_fetch_array($result))
{ echo $row['FirstName'] . " " .
$row['LastName']; echo "<br />";
}
mysql_close($con);
?>
The example above stores the data returned by the mysql_query() function in the $result
variable.
Next, we use the mysql_fetch_array() function to return the first row from the recordset as an
array. Each call to mysql_fetch_array() returns the next row in the recordset. The while loop
loops through all the records in the recordset. To print the value of each row, we use the PHP
$row variable ($row['FirstName'] and $row['LastName']).
The output of the code above will be:
Peter Griffin
Glenn Quagmire
mysql_select_db("my_db", $con);
while($row = mysql_fetch_array($result))
{ echo "<tr>"; echo "<td>" .
$row['FirstName'] . "</td>"; echo "<td>"
. $row['LastName'] . "</td>"; echo
"</tr>";
} echo
"</table>";
mysql_close($con);
?>
Prepared by:
Mr. James Batista A. L
Jamesbatista2012@gmail.com
ICT Instructor: DBVTC
Juba South Sudan