Scripting Language Unit 7 To 9 Lab Report
Scripting Language Unit 7 To 9 Lab Report
1
TITLE:
A Program to create multiple objects under common class using constructor and destructor in
PHP.
THEORY:
Constructor in PHP:
Constructor is a method defined inside a class which is called automatically as soon as an
object is created. It provides values for member functions and member variables.
Syntax:
__construct(arg1, arg2,.....)
{
//body
}
Destructor in PHP:
Destructor is a method defined inside a class which is called automatically as soon as the
script ends. It destroys the object.
Syntax:
__destruct()
{
//body
}
SOURCE CODE:
<?php
class Rec{
private $length;
private $breadth;
function __construct($len,$bre) {
$this->length = $len;
$this->breadth =$bre;
}
function __destruct() {
$area= $this->length * $this->breadth;
echo "Area of {$this->length} by {$this->breadth} Rectangle is {$area}. <br><br> ";
}
}
$r1 = new Rec(5,6);
$r2 = new Rec(12,24);
$r3 = new Rec(3,16);
?>
OUTPUT:
CONCLUSION:
Hence, multiple objects under common class can be created using __construct() and
__destruct() functions in PHP.
LAB 7.2
TITLE:
A PHP program to access and modify protected data members from outside class using getter
and setter methods.
THEORY:
Encapsulation in PHP:
Encapsulation or Information hiding is the process of combining data members and member
functions to form a single unit to protect from external resources is called encapsulation. To
achieve, encapsulation two key points must be taken into consideration:
i) declaring variable, we want to hide as private.
ii) using getters and setters to access & modify protected members outside class.
SOURCE CODE:
<?php
class Rec{
private $length;
private $breadth;
public function setValue($len,$bre) {
$this->length = $len;
$this->breadth =$bre;
}
public function getArea() {
echo "<br> Area of Rectangle is ".($this->length * $this->breadth);
}
}
$r1=new Rec();
$r1->setValue(5,8);
$r1->getArea();
?>
OUTPUT:
CONCLUSION:
Thus, we conclude that protected members of class can be accessed and modified using
getter and setter methods in PHP.
LAB 7.3
TITLE:
A program to illustrate polymorphism in PHP using abstract class.
THEORY:
Polymorphism in PHP:
Polymorphism is an OOP pattern that enables multiple classes to have methods with the same
name but different implementation/body. It can be implemented in 2 ways: -
i) Using abstract class
ii) Using Interface
SOURCE CODE:
<?php
abstract class P{
abstract function greet();
}
class C1 extends P {
function greet(){
echo"Hello";
}
}
class C2 extends P{
function greet(){
echo "<br>Namaste";
}
}
class C3 extends P{
function greet(){
echo "<br>Hi";
}
}
$ob=new C1();
$ob1 = new C2();
$ob2 = new C3();
$ob->greet();
$ob1->greet();
$ob2->greet();
?>
OUTPUT:
CONCLUSION:
Thus, we conclude that polymorphism can be implemented in PHP using abstract class.
LAB 7.4
TITLE:
A program to show that multiple exceptions can be handled in PHP by extending the class
Exception.
THEORY:
Exception Handling in PHP:
Unexpected outcome of program which disrupts the normal flow of program but can be
handled by program itself is known as exceptions. The main difference between error and
exception is that errors can’t be handled by the program itself while exceptions can be.
Multiple exceptions can be handled in PHP by extending the exception class.
CONCLUSION:
Thus, multiple exceptions in PHP can be handled by simply extending the exception class.
LAB 8
TITLE:
A program to store HTML form inputs to MySQL database table without page reload.
THEORY:
AJAX:
AJAX stands for Asynchronous JavaScript and XML. With the help of AJAX, we can
communicate with the server without page reload.
Types of AJAX:
i) Asynchronous AJAX: allows execution of next line of code before server response
received.
ii) Synchronous AJAX: stops execution of next line of code until server response
received.
AJAX advantages:
i) makes webpage more user interactive.
ii) faster execution
AJAX disadvantages:
i) Search Engine Optimization (SEO) is affected (because no specific URL’s of page)
AJAX examples:
i) Facebook like and comments
ii) Country and City Dropdown
SOURCE CODE:
//index.php
<!DOCTYPE html>
<html>
<head><title>ajax with php and mysql</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<body>
<h1>Enter product</h1>
<form method='post' action='insert.php'>
<input type = 'text' id = 'name' name = 'productname' placeholder='enter product
name'><br><br>
<input type = 'text' id = 'brand' name = 'brandname' placeholder='enter brand
name'><br><br>
<input type = 'number' id = 'quantity' name = 'quantity' placeholder='quantity'>
<button>submit</button>
</form>
<p id="m"></p>
<script>
$("form").submit(function(e){
e.preventDefault();
$.post(
'insert.php',
{productname: $('#name').val(),
brandname: $('#brand').val(),
quantity: $('#quantity').val()},
function(result){
if(result)
$('#m').html('values inserted successfully');
else
$('#m').html('error occured');}
);
});
</script>
</body>
</html>
//insert.php
<?php
$name = $_POST["productname"];
$brand = $_POST["brandname"];
$qty = $_POST["quantity"];
$con = new mysqli('localhost','root','','compan');
if($con->connect_error)
echo "connection error";
Run 2:
CONCLUSION:
Thus, HTML form data can be stored to the MySQL database table without page reload
using AJAX.
LAB 9.1
TITLE:
A program to hide and show the HTML element using jquery.
THEORY:
We use hide() and show() methods to hide and show HTML elements in jQuery.
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
SOURCE CODE:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
<body>
<p id="m">Hello World</p>
<button id="h">Hide</button>
<button id="s">Show</button>
<script>
$(document).ready(function(){
$("#h").click(function(){$("#m").hide();});
$("#s").click(function(){$("#m").show();});
});
</script>
</body>
</html>
OUTPUT:
CONCLUSION:
Thus, It is concluded that we can hide and show the HTML element using hide() and show()
methods in jQuery.
LAB 9.2
TITLE:
A program to drop one HTML element into another HTML element using jqueryUI.
THEORY:
One HTML element can be dropped into other HTML element by using droppable() and
draggable() methods in jQuery.
Syntax:
$(selector for larger element).droppable ( );
$(selector for smaller element).draggable ( );
SOURCE CODE:
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
#drag {
width: 50px; height: 50px; padding: 0.5em; float: center; background-color: red; color:
black; border: 2px solid black; text-align:center;
}
#drop {
width: 100px; height: 100px; padding: 0.5em; float: center; background-color:green;
color: black; border: 2px solid black; text-align:center;
}
</style>
<script>
$(function() {
$( "#drag" ).draggable();
$( "#drop" ).droppable();
});
</script>
</head>
<body>
<div id="drag">
<p>Hello</p>
</div><br><br>
<div id="drop">
<p>World</p>
</div>
</body>
</html>
OUTPUT:
CONCLUSION:
Thus, one HTML element can be dropped into another HTML element using draggable() and
droppable() methods in jQueryUI.