XML, Ajax and PHP
XML, Ajax and PHP
You can communicate between two platforms which are generally very difficult.
The main thing which makes XML truly powerful is its international acceptance.
Many corporation use XML interfaces for databases, programming, office application mobile phones and more. It is due to its platform
independent feature.
The same XML data can be used in many different presentation scenarios.
Because of this, with XML, there is a full separation between data and presentation.
Advantages of XML
XML is Reusable.
XML is an open format that can be processed by any XML aware application like a browser, word processor, etc.
XML is compatible with all kinds of languages that used over internet like JSP, ASP, etc.
Disadvantages of XML
Sometime XML is not so good at handling large amount o data.
XML can quickly become difficulty when lot of information is included in one file.
Features of XML
XML is widely used in the era of web development. It is also used to simplify data storage and data sharing.
The main features of XML are given below.
XML was designed to carry data - with focus on what data is.
HTML was designed to display data - with focus on how data looks.
XML tags are not predefined like HTML tags are. This means XML Does not use Predefined tags.
Tags in the XML are custom tags that can be invented by the author of the XML document.
HTML works with predefined tags like <p>, <h1>, <table>, etc.
With XML, the author must define both the tags and the document structure.
When displaying data in HTML, you should not have to edit the HTML file when the data changes.
With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.
Rules for well formed XML
The first line is the XML declaration. It defines the XML version (1.0)
and the encoding used (ISO-8859-1 = Latin-1/West European
It must begin with the XML declaration i.e. XML Prolog.
o
character set).
It must have one unique root element.
o
An XML tree starts at a root element and branches from the root to child elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Example of an XML document
<Company>
<Employee>
<FirstName >Harendra</FirstName>
<LastName>Negi</LastName>
<ContactNo>9876543210</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Dehradun</City>
<State>Uttarakhand</State>
<Zip>243001</Zip>
</Address>
</Employee>
</Company>
Tree Rules
These rules are used to figure out the relationship of the elements.
In the previous example “Company" is the root element and all the other elements are the descendants of "Company".
Ancestors: The containing element which contains other elements is called "Ancestor" of other element.
In the previous example root element (Company) is ancestor of all other elements.
Q1. Construct XML Tree. Q2. Write XML code which needs to follow the given structure.
<Employee>
<Name>
<First name>Brian</First name>
<Last name>Lara<</Last name>
</Name>
<Contact>
<Mobile>9800000000</Mobile>
<Landline>020222222</Landline>
</Contact>
<Address> Q3. Write XML code which needs to follow the given structure.
<City>Pune</city>
<Street>Tilak road</Street>
<Zip code>4110</Zip code>
</Address>
</Employee>
DOM
The Document Object Model (DOM) is the foundation of XML.
XML documents have a hierarchy of informational units called nodes; DOM is a way of describing those nodes and the relationships between
them.
This hierarchy allows a developer to navigate through the tree looking for specific information. Because it is based on a hierarchy of
information, the DOM is said to be tree based.
The HTML DOM defines a standard way for accessing and manipulating HTML documents. It presents an HTML document as a tree-
structure.
The XML DOM defines a standard way for accessing and manipulating XML documents. It presents an XML document as a tree-structure.
Understanding the DOM is a must for anyone working with HTML or XML.
The XML DOM is a standard for how to get, change, add, and delete XML elements.
Example of an XML document
<Company>
<Employee>
<FirstName>Harendra</FirstName>
<LastName>Negi</LastName>
<ContactNo>9876543210</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Dehradun</City>
<State>Uttarakhand</State>
<Zip>243001</Zip>
</Address>
</Employee>
</Company>
What is DTD
It is used to define document structure with a list of legal elements and attributes.
It contains a list of legal elements and define the structure with the help of them.
With a DTD, you can verify that the data you receive from the outside world is valid.
When you are working with small XML files, creating DTDs may be a waste of time.
•Internal DTD
•External DTD
Internal DTD
A DTD is referred as an internal DTD if elements are declared within the XML files.
To refer it as an internal DTD, standalone attribute in xml declaration must be set to yes.
This means, the declaration works independent of external source.
Syntax:
Where root-element is the name of root element and element declaration is where you want to declare the elements.
Eg:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE address[
<!ELEMENT addresss (name,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>Harendra</name>
<phone>1234567</phone>
</address>
<!DOCTYPE address: defines root element of document.
<!ELEMENT address: defines that address must contain name, and phone.
#PCDATA: is a parse-able text data or parse character data. It is String data type.
It means characters are to be parsed by XML. XML parsers are used to parse all text in an XML document.
1.DOM
To refer it as external DTD, standalone attribute in the XML declaration must be set as no.
Syntax:
SYSTEM is an identifier which enables you to specify the location of an external file containing DTD declarations.
XML CODE DTD CODE: employee.DTD
AJAX allows you to send and receive data asynchronously without reloading the web page. So it is fast.
AJAX allows you to send only important information to the server not the entire page. So only valuable data from the client
side is routed to the server side. It makes your application interactive and faster.
A browser built-in XMLHttpRequest object (to request data from a web server)
JavaScript and HTML DOM (to display or use the data)
AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means
that it is possible to update parts of a web page, without reloading the whole page.
How AJAX Works
A synchronous request blocks the An asynchronous request doesn’t block the client i.e.
client until operation completes i.e. browser is responsive.
browser is unresponsive.
At that time, user can perform another operations
In such case, JavaScript engine of the also. In such case, JavaScript engine of the browser is
browser is blocked. not blocked.
An object of XMLHttpRequest is used for asynchronous communication between client and server.
The XMLHttpRequest object can be used to exchange data with a server behind the scenes. This means that it is possible to
update parts of a web page, without reloading the whole page.
Method Description
void open(method, URL) opens the request specifying get or post method and url.
void open(method, URL, async) same as above but specifies asynchronous or not.
void open(method, URL, async, username, password) same as above but specifies username and password.
•It handles dynamic content, database as well as session tracking for the website.
•As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.
•PHP can handle the forms, such as - collect the data from users using forms, save it into the database, and
return useful information to the user. For example - Registration form.
•PHP is very popular language because of its simplicity and open source.
PHP Installation
Most people would prefer to install a all-in-one solution.
The PHP Community Provides Some types of Software Server solution under The GNU (General Public
License).
These are the following:
1.WAMP Server
2.LAMP Server
3.MAMP Server
4.XAMPP Server
All these types of software automatically configure inside operating system.
After installation, it having PHP, MySQL, Apache and operating system base configuration file, it doesn't need to
configure manually.
<?php /*
This is Multiline Comments
# This is the next line comment */
A Variable is simply a container i.e. used to store both numeric and non-numeric information.
Variables in PHP starts with a dollar($) sign, followed by the name of the variable.
The variable name must begin with a letter or the underscore character.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
Example
<?php
$txt ="Hello World!";
$x=16;
?>
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
<?php
$str="hello string";
$x=200;
$y=44.6;
<?php
echo $a . “ “ . $b;
?>
?> ?>
Output Output
Harendra Singh int 300
Variable Scope
The scope of a variable is defined as its range in the program under which it can be accessed.
In other words, "The scope of a variable is the portion of the program within which it is defined and can be accessed.“
2.Global variable
3.Static variable
Local variable
The variables that are declared within a function are called local variables for that function.
These local variables have their scope only in that particular function in which they are declared.
This means that these variables cannot be accessed outside the function, as they have local scope.
<?php
<?php function disp()
function disp() { {
$a = "PHP";
$a = 4; //local variable
echo “Unit V is based on: " .$a;
echo "Local variable = ". $a; }
} disp();
echo $a;
disp();
//use of local variable outside the function will generate an error
?>
?>
Output:
Output:
Local variable = 4
Unit V is based on: PHP
Undefined variable: a
Global variable
The global variables are the variables that are declared outside the function.
To access the global variable within a function, use the GLOBAL keyword before the variable.
However, these variables can be directly accessed or used outside the function without any keyword.
Therefore there is no need to use any keyword to access a global variable outside the function.
Example:
Example:
<?php
<?php
$a = "Java";
$a = "Java";
function disp() {
function disp()
global $a;
{
echo "Unit V is based on : ". $a;
echo $a;
echo "<br/>"; }
}
disp();
disp();
echo " Outside the function " . $a;
?> Output: Unit V is based on : Java
?> Output: Undefined variable
Name outside the function: Java
Use $GLOBALS instead of global
Another way to use the global variable inside the function is predefined $GLOBALS array.
If two variables, local and global, have the same name, then the local
Example:
variable has higher priority than the global variable inside the function.
<?php
Example:
$num1 = 15;
<?php
$num2 = 18;
$a = 5;
function add()
function disp()
{
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
$a = 7;
echo "Sum of global variables is: " . $sum; echo " Value of a = " . $a;
}
}
disp();
add();
?>
?>
Output: Output:
We use the static keyword before the variable to define a variable, and this variable is called as static variable.
Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope.
<?php
function disp() OUTPUT:
{ Static: 2
static $num1 = 1; Non-static: 4
Static: 3
$num2 = 3; Non-static: 4
$num1++;
$num2++; You have to notice that $num1 regularly increments after each function
echo "Static: " .$num1 ."</br>"; call, whereas $num2 does not. This is why because $num1 is not a static
echo "Non-static: " .$num2 ."</br>"; variable, so it freed its memory after the execution of each function call.
}
disp();
disp();
?>
Form
Form is used to take input from users.
In PHP if you want to take input from keyboard and display the output according to input, in this case you have to use html form.
To get form data, we need to use PHP superglobals $_GET and $_POST.
To retrieve data from get request, we need to use $_GET, for post request $_POST.
Get Form
Get request is the default form request.
The data passed through get request is visible on the URL browser so it is not secured.
You can send limited amount of data through get request.
Post Form
Post request is widely used to submit form that have large amount of data such as file upload, image upload, login form, registration form etc.
The data passed through post request is not visible on the URL browser so it is secured.
You can send large amount of data through post request.
Example: Get Request Example: Post Request
File: index.html File: index2.html
<form action="p11home.php">
<form action="login.php" method="post">
Name <input type="text" name="name"> Name:<input type="text" name="name"/>
File: login.php
File: p11home.php <?php
<?php
$name1=$_GET["name"]; $name=$_POST["name"];
$password=$_POST["password"];
echo"Welcome, $name1";
echo "Welcome: $name, your password is: $password";
?> ?>
PHP isset()
isset() function is used to check if a variable exists in the code or not.
We use the isset function to check if any variable that is passed, exists in the code and also possess some value.
If a variable have some value then it is said to be set and if it doesn't have any value stored in it that is contains NULL then it is said to be unset.
If we pass any variable in the isset function, it returns the result as either True or False.
If the variable that we have passed has been declared and also contain some value other than NULL then it will be returning True as the result
but if the variable is not declared or defined in the code that means either there is no such variable present in the code or it has a NULL value
then it will return False as the result.
We can check multiple variables at a time by passing multiple variables in the Isset().
In this case, function will check each and every variable whether it is set or unset and if all the variables are set then it will return True and if
any of the variable is unset then it will return False.
Syntax:- isset(variable);
<?php <?php
$a = 5; $a=5;
//variable 'a' is declared & defined $b=6;
$c=NULL;
if (isset($a)) //False because 'c' is NULL
{ //it will return true or false
Each time when client sends request to the server, cookie is embedded with request. Such way, cookie can be received at
the server side.
PHP session is used to store and pass information from one page to another temporarily (until user close the website).
PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g.
username, product code, product name, product price etc from one page to another.
PHP session creates unique user id for each browser to recognize the user and avoid conflict between multiple browsers.