Practical File
Of
             Web Development using PHP
                   Session: 2022-2025
Submitted to: - Harinder singh    Submitted by: - Eklavya nayak
                                  INDEX
S.No.                 Description                     Page    Date        Remarks
                                                      No.
 1.                                                          05.08.2024
        To design a simple XML document with           1.
        new tags.
 2.                                                          12.08.2024
        Represent Library books data using XML.
                                                       3.
 3.                                                          02.09.2024
        Understanding XML schema and its
                                                       5.
 4.     various data types and tags                          09.09.2024
        Creation of Web pages using PHP               10.
 5.                                                          23.09.2024
        To store a cookie using PHP on client side.   18.
 6.                                                          30.09.2024
        To save the user session on server side.      20.
 7.                                                          07.10.2024
        To connect MySQL database using PHP,          21.
        reading the database and writing values
        into the database.
 8.     To implement web pages using the AJAX.        29.    14.10.2024
                       ACKNOWLEDGEMENT
I want to express my gratitude towards “Mr. Harinder Singh” for his
constant support and guidance.
(NAME)
(BRANCH)
(ROLL NO)
                                  Practical 1.                            1.
Description:
To design a simple XML document with new tags.
What is XML?
 XML stands for extensible markup language. A markup language is a set of codes, or tags,
that describes the text in a digital document. The most famous markup language is hypertext
markup language (HTML), which is used to format Web pages.
Creating a simple document in XML:
Source Code: -
<?xml version="1.0" encoding="UTF-8"?>
<college>
<class1>
<name> Nilesh Chhabra </name>
<rollno> 9522</rollno>
<name> Aditya </name>
<rollno>9502</rollno>
<name> Dilpreet Singh </name>
<rollno>9510</rollno>
<name> Devansh </name>
<rollno>9508</rollno>
</class1>
<class2>
<name> Anuj</name>
<rollno>9505</rollno>
<name> Vaibhav</name>
                        Practical 1.                      2.
<rollno>9530</rollno>
<name> Amit </name>
<rollno>9509</rollno>
</class2>
</college>
Output: -
                                       Teacher Remarks:
                                 Practical 2.                    3.
Description:
Represent Library books data using XML.
Source Code: -
<?xml version="1.0" encoding="UTF-8"?>
<Library>
 <book category="Networking">
  <title lang="English"> Computer Network and Security</title>
  <author>NAVEEN KUMARI</author>
  <publisher> Eagle's Publication</publisher>
  <year>2019</year>
  <price> Rs. 178</price>
</book>
  <book category="WEB Development">
  <title lang="English"> Web Development using PHP</title>
  <author>Er. Kirandeep Kaur</author>
  <publisher> Eagle's Publication</publisher>
  <year>2020</year>
  <price>Rs.168</price>
</book>
</Library>
            Practical 2.                      4.
Output: -
                           Teacher Remarks:
                                   Practical 3.                          5.
Description: -
Understanding XML schema and its various data types and tags.
What is XML Schema?
   •   An XML Schema describes the structure of an XML document.
   •   The XML Schema language is also referred to as XML Schema Definition (XSD).
What is the purpose of XML?
The purpose of an XML Schema is to define the legal building blocks of an XML document:
   •   the elements and attributes that can appear in a document.
   •   the number of (and order of) child elements.
   •
   •
       data types for elements and attributes.
       default and fixed values for elements and attributes.
The Schema of Xml is in 2 formats i.e.: -
   1. DTD (Document Type Definition)
   2. XSD (XML Schema Definition)
The difference of DTD and XSD are given below: -
                                    Practical 3.                            6.
Data types in XML: -
   •   String Type
   •   Tokenized Type
   •   Enumerated Type
          1. String Type: - It takes any literal string as a value.
                 ➢ CDATA (Unparsed Character Data): - It contains the text which is
                    not parsed further in an xml document. Tags inside this text are not
                    treated as markup and entities will not be expanded.
                       CDATA section can be used to “Block escape” literal text. CDATA
                       section can appear inside element content and allow < and &
                       character literals to appear.
                       Practical 3.                              7.
      ➢ PCDATA (Parsed Character Data): - XML parsers are used to parse
          all the text in an XML document. PCDATA is the text that will
          be parsed by a parser. Tags inside the PCDATA will be treated
          as markup an entities will be expanded. In other words you
          can say that a parsed character data means the XML parsed
          examines the data and ensures that it doesn’t contain entity,
          if it contains that will be replaced.
2. Tokenized Type: -
      ➢ IDREF: - The value is the id of another element. Works for many to
         one relationship
      ➢ IDERFS: - The values is a list of others ids. Works for one to many
         relationships.
          FOR E.G.
          Source code: -
          <?xml version="1.0" ?>
          <!DOCTYPE song
          [
          <!ELEMENT song (artist,album)>
          <!ELEMENT artist EMPTY>
          <!ATTLIST artist name CDATA #REQUIRED>
          <!ATTLIST artist artistID ID #REQUIRED>
                 Practical 3.                               8.
   <!ELEMENT album EMPTY>
   <!ATTLIST album name CDATA #REQUIRED>
   <!ATTLIST album albumArtistID IDREF #IMPLIED>
   <!ATTLIST album contributingArtistIDs IDREFS #IMPLIED>
   ]>
   <song>
   <artist name="Nick Cave" artistID="NC"/>
   <artist name="Kylie Minogue" artistID="KM"/>
   <artist name="PJ Harvey" artistID="PJH"/>
   <artist name="Shane MacGowan" artistID="SM"/>
   <album name="Murder Ballads"
   albumArtistID="NC" contributingArtistIDs="KM PJH SM"/>
   </song>
   Output: -
➢ NMTOKEN: - The value is a valid XML name. XML name tokens
  may not contain white space, but can start with a letter, digit, ‘.’,
  ‘_’, or ‘:’ and the string xml.
➢ NMTOKENS: - The value is a list of valid xml names.
   Allows multiple NMTOKEN names separated by whitespace.
                                   Practical 3.                              9.
                 ➢ ENTITY: - This attribute is used to indicate the attribute which will
                     represent an internal entity used in the document. Entity must be
                     declared before they can be used.
                 ➢ ENTITIES: - This attribute is used to indicate the attribute which will
                    represent external entities used in the document.
          3. Enumerated Type: - this has a list of predefined values in its declaration. Out
             of which, it must be one value.
                 ➢ NOTATION: - The NOTATION Declaration can be used within an
                     DTD to qualify the data contained within an external entity (non-
                     XML) file.
                 ➢ XML: : -     The value is a predefined xml values.
                 ➢ Enumeration (en1|en2|..) : -The value must be one from an
                     enumerated list.
What are XML Tags ?
XML tags are used to specify a name for a given piece of information . It is a mean of
identifying data. Data is marked up using tags. Angular brackets (< or >) are used for opening
and closing the name of XML tags . the tags occur in pairs if they are containers tags, and
each pair consist of opening and closing tags. The XML allows the user to create his own
tags. For e.g.
                     <EMP_NAME> ABC </EMP_NAME>
                                                   Teacher Remarks:
                              Practical 4.           10.
Description: -
Creation of Web pages using PHP.
Installation of Xampp Server : -
Step 1: -
Go to https://www.apachefriends.org/
                                  or search Xampp.
Step 2: -
Choose your Operating System.
Step 3: -
A .exe file will automatically downloaded.
                             Practical 4.   11.
Step 4: -
Now execute the .exe file.
Step 5: -
Now click on Next.
                             Practical 4.   12.
Step 6: -
Select all components shown blow: -
Now click on Next.
Step 7: -
Now select the path and click on next.
                             Practical 4.           13.
Step 8: -
Now select your language and click on next again.
                              Practical 4.               14.
Step 9: -
Now Xampp is start installing(it will take some time).
                       Practical 4.   15.
Step 10: -
Now Click on finish.
                                      Practical 4.   16.
Creating a Web page using PHP: -
Source Code: -
<!DOCTYPE html>
<html>
  <style>
     body{
         background: #fff
     }
     h1{
         color: black;
         font-size: 40px;
         font-variable: small-caps;
     }
  </style>
<body>
  <h2> Creating WEB PAGE using PHP</h2>
<?php
echo"Example of A PHP Page";
?>
</body>
</html>
            Practical 4.                      17.
Output: -
                           Teacher Remarks:
                                   Practical 5.                               18.
Description: -
To store a cookie using PHP on client side.
What is a cookie?
      Cookies are small files which are stored on a user’s computer.
  •
      They are used to hold a modest amount of data specific to a particular client and
  •   website and can be accessed either by the web server or by the client computer.
  •    If a cookie is created in a particular website and you visit another website later, the
      latter would not be able to read the contents from the first website.
      As per HTTP protocol, size of the cookies cannot be greater than 4KB.
  •
Syntax: -
        setcookie(name, value, expire, path, domain, security);
Creating a Cookie: -
Source code: -
                      Practical 5.                      19.
Output: -
Reading a cookie: -
                                     Teacher Remarks:
                                   Practical 6.                             20.
Description: -
To save the user session on server side.
What is a Session?
   •   A PHP session is easily started by making a call to the session_start() function.
       This function first checks if a session is already started and if none is started
       then it starts one. It is recommended to put the call to session_start() at the
   •   beginning of the page. Session variables are stored in associative array called
       $_SESSION[]. These variables
       can be accessed during lifetime of a session.
Creating a Session: -
Source Code: -
            Practical 6.                      20.
Output: -
                           Teacher Remarks:
                                Practical 7.                           21.
Description: -
To connect MySQL database using PHP, reading the database and writing
values into the document.
To connect to MySQL database using PHP: -
There are three ways in which we can connect to MySQL from PHP ;
1. MySQLi (object oriented)
2. MySQLi (procedural)
3. PDO.
Here, only MySQL object-oriented procedure is used given below.
Using MySQL object-oriented procedure : MySQLi object-oriented procedure can be used to
establish a connection to MySQL database from PHP script.
Code with MySQL object-oriented procedure :-
Output :-
                               Practical 7.                            22.
To create database in MySQL Server : -
The CREATE DATABASE statement is used to create a database in MySQL.
The following example creates a database named “myDB”.
Code to create database :-
Output :-
                                 Practical 7.                    23.
Create a MySQL table using MySQLi: -
The CREATE TABLE statement is used to create a table in MySQL.
                        Code to create table in a database :-
            Practical 7.   24.
Output :-
                                  Practical 7.                            25.
The table created is empty as shown below :-
Writing or Inserting data into table of MySQL data using PHP: -
After a database and a table have been created, we can start adding data in
them. Here are some syntax rules to follow : 1. The SQL query must be
quoted in PHP.
2. String values inside the SQL query must me quoted.
3. Numeric values must not be quoted.
4. The word NULL must not be quoted.
The INSERT INTO statement is used to add new records to a MySQL table.
                                   Practical 7.       26.
Code to insert a record or row in the data table :-
Output :-
                                   Practical 7.                             27.
•   Likewise, more rows can be inserted, as shown below.
                                 Fig. Insert more rows.
7.5. Reading data from table of MySQL data using PHP
Retrieval means to get/show/read the data stored in the database table to the user. For this
purpose. “SELECT” command is used.
Syntax : SELECT column_name(s) FROM table_name
or we can use the * character to select all columns from a table:
Syntax : SELECT * FROM table_name
                                 Practical 7.                      28.
Code to retrieve data from database table :-
Output :-
                                                Teacher Remarks:
                                   Practical 8.                          29.
Description: - To implement web pages using the AJAX.
To create AJAX example, you need to use any server-side language e.g. Servlet, JSP,
PHP, ASP.Net etc. Here, we are using PHP for generating the server-side code. The
following example demonstrates how a web page can communicate with a web
server while a user types characters in an input field:
1. Install XAMPP server to run PHP code.
2. Make sure that Apache and MySQL services of XAMPP server are ON
3. Check if the server is running as shown below
   • Write the following code: -
          ➢ HTML side: -
                    Practical 8.                          30.
<html>
<head>
<script>
function showHint(str)
{
if (str.length == 0)
{
document.getElementById("txtHint").innerHTML="";
 return;
}
else
{
varxmlhttp = new XMLHttpRequest();
 xmlhttp.onreadystatechange = function()
 {
if (this.readyState 4 &&this.status 200)
    {
      document.getElementById("btHint").innerHTML = this.responseText;
    }
}; xmlhttp.open("GET", "getlink.php?b=" + str,
true); xmlhttp.send(); } } </script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
                          Practical 8.   31.
➢ AJAX/PHP side: -
   <?php
   // Array with names
   $a[] = "Anna";
   $a[] = "Brittany";
   $a[] = "Cinderella";
   $a[]= "Diana";
   $a[] = "Eva";
   $a[] = "Fiona";
   $a[]= "Gunda";
   $a[]= "Hege";
   $a[]= "Inga";
   $a[]= "Johanna";
   $a[]= "Kitty";
   $a[]= "Linda";
   $a[] = "Nina";
   $a[] = "Ophelia";
   $a[] = "Petunia";
   $a[] = "Amanda";
   $a[] = "Raquel ";
   $a[] = "Cindy";
   $a[] = "Doris";
   $a[]= "Eve";
   $a[] = "Evita";
   $a[] = "Sunniva" ;
   $a[] = "Tove";
   $a[] = "Unni";
   $a[] = "Violet";
   $a[] = "Liza";
   $a[] = "Elizabeth";
   $a[]= "Ellen";
   $a[] = "Wenche";
   $a[] = "Vicky";
                      Practical 8.                      32.
//get the q parameter from URL
$b = $_REQUEST["b"];
$hint= "";
// lookup all hints from amay if $q is different from
if ($b!== "")
{
 $b= strtolower($b);
 $len=strlen($b);
foreach($a as $name)
{
    if (stristr($b,substr($name, 0, $len)))
if ($hint==="")
{
$hint="$name";
}
else
{
 $hint .=",$name";
     }
   }
 }
 echo $hint===""?"no suggestion":$hint;
?>
                            Practical 8.                      33.
Output: -
If suggestion matches: -
If suggestion doesn’t matches: -
                                           Teacher Remarks: