WT Unit 4 Server Side Technology 2
WT Unit 4 Server Side Technology 2
“SERVER SIDE
TECHNOLOGYIES - II”
By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.
Note: The material to prepare this presentation has been taken from internet and are generated only
PHP
AJAX
PHP
Outline
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code are executed on the server, and the result is returned
to the browser as plain HTML
• PHP can create, open, read, write, delete, and close files on the server
• With PHP you are not limited to output HTML. You can output images,
PDF files, and even Flash movies. You can also output any text, such as
XHTML and XML.
Features /Advantages of PHP
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X,
etc.)
• PHP is compatible with almost all servers used today (Apache, IIS, etc.)
• Advantages :
• Simple , Interpreted , Faster , Open Source , Platform Independent ,
Efficiency, Flexibility.
PHP Syntax
• Basic PHP Syntax
• <?php
// PHP code goes here
?>
<?php
echo "Hello World!";
?>
</body>
</html>
sample code –
Example 2 variable declaration
<html>
<body>
Out Put
<?php
$txt = "Hello world!"; "Hello world!"
$x = 5; 5
10.5
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
PHP Variables
• Rules for PHP variables:
• A variable starts with the $ sign, followed by the name of the variable
• Variable names are case-sensitive ($age and $AGE are two different
variables)
sample code –
Example 3- To output text and a variable
• <html>
• <html>
<body>
<body>
<?php
<?php
$txt = "W3Schools.com";
$txt = "W3Schools.com";
echo "I love " . $txt . ;
echo "I love $txt";
?>
?>
</body>
</body>
</html>
</html>
Program for addition oftwo numbers
• <body>
• <?php
• $n1=5;
• $n2=6;
• $sum=$n1+$n2;
• ?>
• </body>
PHP Variables Scope
• In PHP, variables can be declared anywhere in the script.
• The scope of a variable is the part of the script where the variable
can be referenced/used.
• global
• static
PHP Variables Scope-
Global and Local Scope
Example Out put
<?php • Variable x inside function is:
$x = 5; // global scope
• Variable x outside function is: 5
function myTest() {
echo "Variable x inside function is: $x";
}
myTest();
A variable declared outside a function has a GLOBAL SCOPE and can only
be accessed outside a function:
PHP Variables Scope-
Global and Local Scope
Example Out put
<?php • Variable x inside function is: 5
function myTest() • Variable x outside function is:
{
$x = 5; // local scope
echo "Variable x inside function is: $x";
}
myTest();
echo "Variable x outside function is: $x";
?>
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
PHP Variables Scope-
The global Keyword
Example Out put
<?php 15
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
Less than
<= $x <= $y Returns true if $x is less than or equal to $y
or equal to
Outline
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
• If else
Conditional • Elseif ladder
Statements • Switch case
• While
Loop • Do while
Statements • For
• Foreach
If…else
Syntax Example
• If(condition) • <body>
• statements; • <?php
• else • $n=5;
• statements; • If($n%2 == 0)
• Echo “Number is Even”;
• Else
• Echo “Number is Odd”;
• ?>
• <body>
Elseif
Syntax Example
• If(condition) • <body>
• <?php
• statements; • $day=date(“l”);
• Elseif(condition) • If($day == “Saturday”))
• statements; • Echo “Happy Weekend”;
• Elseif($day == “Sunday”))
• Else
• Echo “Happy Sunday”;
• statements; • Else
• Echo “Nice Working day”;
• ?>
• <body>
Example
Switch case
• <?php
Syntax $favcolor = "red";
switch ($favcolor) {
• Switch(expression) case "red":
• { echo "Your favorite color is red!";
• Case constant_expression: break;
• statements;
• break;
• case "blue":
echo "Your favorite color is blue!";
• Default: break;
• statements;
default:
• }
• echo "Your favorite color is neither
red, blue!";
}
?>
While Loop
Syntax Example
• do { • <?php
code to be executed; $x = 1;
} while (condition is true); do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
For Loop
Syntax Example
{
echo "$value <br>";
}
?>
Outline
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
writeMsg();
?>
</body>
Parameterized Functions
Example Output
• <html> • Sum is 30
• <body>
• <?php
function Add($a,$b) {
$sum=$a+$b;
echo “Sum is $sum";
}
Add(10,20);
?>
• </body>
• </html>
Returning value through function
Example Output
• <html> • Sum is 30
• <body>
• <?php
function Add($a,$b) {
$sum=$a+$b;
return $sum;
}
$Result=Add(10,20);
echo “Sum is $Result";
?>
• </body>
• </html>
Setting default valuesfor function
parameter
Example Output
<html>
<body> • Sum is 310
<?php • Sum is 30
function Add($a,$b=300)
{
$sum=$a+$b;
echo “Sum is $sum";
}
Add(10);
Add(10,20);
?>
</body>
</html>
Dynamic Function Calls
Example Output
<<html>
Hello How R U?
<body>
<?php
function Hello() {
echo "Hello How R U?";
}
$fh = "Hello";
$fh();
?>
</body>
</html>
Outline
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
Introduction to PHP,
Features,
sample code,
PHP script working,
PHP syntax,
Functions,
String manipulation,
Form handling,
• Example
• <?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
2. $age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Arrays- Associative Arrays
• Example
• <?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
• Example
• $cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
);
Functions on Array
Array
<?php
$a = (
array ('a' => 'apple', 'b' => 'banana‘) [a] => apple
print_r ($a); [b] => banana
?> )
Functions on Array:extract()
extract($my_array);
echo $Rno;
echo $Name;
echo $Class;
?>
Functions on Array:compact()
print_r($result);
?>
Functions on Array:sort()
Example
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
print_r ($numbers);
?>
Outline
Introduction to PHP,
Features,
sample code,
PHP script working,
PHP syntax,
Functions,
String manipulation,
Form handling,
Forms are used to get input from the user and submit it to the web server for
processing.
Create a form
We will use HTML tags to create a form. Below is the minimal list of things
you need to create a form.
Opening and closing form tags <form>…</form>
Form submission type POST or GET
Submission URL that will process the submitted data
Input fields such as input boxes, text areas, buttons,checkboxes etc.
FORM HANDING IN PHP
PHP POST method
This is the built in PHP super global array variable that is used to get values
submitted via HTTP POST method.
The array variable can be accessed from any script in the program; it has a
global scope.
This method is ideal when you do not want to display the form post values in
the URL.
A good example of using post method is when submitting login details to the
server.
HERE,
It has the following syntax.
<?php “$_POST[…]” is the PHP array
$_POST['variable_name']; “'variable_name'” is the URL variable
?> name.
FORM HANDING IN PHP
PHP GET method
This is the built in PHP super global array variable that is used to get values
submitted via HTTP GET method.
The array variable can be accessed from any script in the program; it has a
global scope.
This method displays the form values in the URL.
It’s ideal for search engine forms as it allows the users to book mark the
results.
It has the following syntax. HERE,
Has lower performance compared to Php_GET Has high performance compared to POST method
method due to time spent encapsulation the dues to the simple nature of appending the values
Php_POST values in the HTTP body in the URL.
Supports many different data types such as string, Supports only string data types because the values
numeric, binary etc. are displayed in the URL
Results cannot be book marked Results can be book marked due to the visibility of
the values in the URL
POST VS GET IN PHP
Form Handling- using Post Method
a.html Welcome.php
<html> <html>
<body> <body>
<form action="welcome.php"
method="post"> Welcome
Name: <?php
<input type="text" name="name"> echo $_POST["name"];
<br> ?>
<input type="submit">
</form> </body>
</body> </html>
</html>
Form Handling- using Get Method
a.html Welcome.php
<html> <html>
<body> <body>
<form action="welcome.php"
method=“get"> Welcome
Name: <?php
<input type="text" name="name"> echo $_GET["name"];
<br> ?>
<input type="submit">
</form> </body>
</body> </html>
</html>
Form Handling-
Difference between get and post method
• $_GET is an array of variables passed to the current script via the
URL parameters.
• $_POST is an array of variables passed to the current script via the
HTTP POST method.
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
• Syntax
• setcookie(name, value, expire, path, domain, secure, httponly);
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Sessions
• A session is a way to store information (in variables) to be used
across multiple pages.
• Unlike a cookie, the information is not stored on the users computer.
<?php
session_start(); ?>
<html>
<body>
<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>
</body>
</html>
Sessions: Destroy a Session
• To remove all global session variables and destroy the session, use
session_unset() and session_destroy():
• Example-
<?php
session_start(); ?>
<html>
<body>
<?php
session_unset(); // remove all session variables
session_destroy(); // destroy the session
?>
</body>
</html>
Outline
Introduction to PHP,
Features,
sample code,
PHP syntax,
Functions,
String manipulation,
Form handling,
<?php
// Create connection
$conn = new mysqli(“localhost”, “root”, “”);
//MySQLi extension (the "i" stands for improved)
// Check connection
if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';
?>
Select Data From a MySQL Database
<?php
$conn = mysqli_connect(‘localhost’, ‘root’, ‘root’, ‘db1’);
if(!$conn){
die(mysqli_connect_error());
}
echo 'Connected successfully<br>';
PHP
WAP & WML,
AJAX
WAP & WML
WAP- Introduction
WAP stands for Wireless Application Protocol
Bearers :
GSM CDMA PHS IS-136 CDPD PDC-P FLEX Etc…
Wireless Application Environment (WAE) -
components
<?xml version="1.0"?>
<wml>
<card id="one" title="First Card">
<p>
This is the first card in the deck
</p>
</card>
<card id="two" title="Second Card">
<p>
Ths is the second card in the deck
</p>
</card>
</wml>
WML Scripts
• Banking:
• Finance:
• Shopping:
• Ticketing:
• Entertainment:
• Weather:
• E- Messaging:
Outline
PHP
WAP & WML,
AJAX
AJAX
AJAX
• What is AJAX?
• JavaScript
• Loosely typed scripting language.
• JavaScript function is called when an event occurs in a page.
• Glue for the whole AJAX operation.
• DOM
• API for accessing and manipulating structured documents.
• Represents the structure of XML and HTML documents.
• CSS
• Allows for a clear separation of the presentation style from the
content and may be changed programmatically by JavaScript
• XMLHttpRequest
• JavaScript object that performs asynchronous interaction with the server.
AJAX – Real TimeExamples
• Here is a list of some famous web applications that make use
of AJAX.
1. Google Maps
• A user can drag an entire map by using the mouse, rather than clicking on
a button.
2. Google Suggest
• As you type, Google offers suggestions. Use the arrow keys to navigate
the results.
3. Gmail
• Gmail is a webmail built on the idea that emails can be more
intuitive, efficient, and useful.
v=document.f1.t1.value;
<body>
var <h1>This is an example of ajax</h1>
url="index.jsp?val="+v; <form name=“f1">
<input type="text" name="t1">
<input type="button" value="ShowTable"
if(window.XMLHttpRequest){ onClick="sendInfo()">
</form>
request=new
<span id="amit"> </span>
XMLHttpRequest(); </body>
} </html>
AJAX Example- index.jsp
<%
int n=Integer.parseInt(request.getParameter("val"));
for(int i=1;i<=10;i++)
out.print(i*n+"<br>");
%>
AXAX Example output
References
• https://www.w3schools.com/php/php_intro.asp
• https://www.w3schools.com/php/php_looping.asp
• https://www.w3schools.com/php/php_if_else.asp
• https://www.w3schools.com/php/php_string.asp
• https://www.w3schools.com/php/php_arrays.asp
• https://www.w3schools.com/php/php_arrays_multi.asp
• https://www.w3schools.com/php/php_forms.asp
• https://www.w3schools.com/php/func_array_extract.asp
• https://www.w3schools.com/php/func_array_compact.asp
• https://www.w3schools.com/php/func_array_in_array.asp
• https://www.w3schools.com/php/php_cookies.asp
• https://www.w3schools.com/php/php_sessions.asp
• https://www.tutorialspoint.com/wap/wap_wml_script.htm
• https://www.w3schools.com/xml/ajax_intro.asp
• https://www.tutorialspoint.com/ajax/ajax_examples.htm
Thank You
[email protected]
Blog : anandgharu.wordpress.com