0% found this document useful (0 votes)
17 views17 pages

Web Tech 2 Practical

The document contains various code snippets and examples for creating XML and JSON files, handling AJAX requests, manipulating DOM elements with jQuery, and performing mathematical operations in PHP. It includes examples of creating and validating XML documents with DTDs, JSON objects, and PHP scripts for file handling, database operations, and session management. Additionally, it provides code for user interface interactions such as hiding, showing, and animating elements on a webpage.

Uploaded by

kccollege0123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

Web Tech 2 Practical

The document contains various code snippets and examples for creating XML and JSON files, handling AJAX requests, manipulating DOM elements with jQuery, and performing mathematical operations in PHP. It includes examples of creating and validating XML documents with DTDs, JSON objects, and PHP scripts for file handling, database operations, and session management. Additionally, it provides code for user interface interactions such as hiding, showing, and animating elements on a webpage.

Uploaded by

kccollege0123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1) Create an XML file with Internal/External DTD representing students data(Name, Roll No,

Email)
Student.dtd
<!ELEMENT students (student+)>
<!ELEMENT student (name, roll_no, email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT roll_no (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
<student>
<name>Mohammed Akib</name>
<roll_no>KCFYCS67</roll_no>
<email>[email protected]</email>
</student>
<student>
<name>Mohammed Amaan</name>
<roll_no>KCFYCS64</roll_no>
<email>[email protected]</email>
</student>
</students>
Validate.php
<?php
echo "Mohammed Akib<br>KCFYCS67<br>";

$xF = 'student.xml';
$dom = new DOMDocument();
$dom->load($xF);

if ($dom->validate()) {
echo "The XML file is valid according to the DTD.";
} else {
echo "The XML file is NOT valid according to the DTD.";
}
?>
Output

2) Create a simple XML document representing a library with books,


including title, author, and year published and Write a DTD for the
XML document created.
Library.dtd
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, year_published)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT year_published (#PCDATA)>
Library.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book>
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<year_published>2000</year_published>
</book>
<book>
<title>The Fault in the stars</title>
<author>John Green</author>
<year_published>2020</year_published>
</book>
</library>
Validate2.php
<?php
echo "Mohammed Akib<br>KCFYCS67<br>";
$xmlFile = 'library.xml';
$dom = new DOMDocument();
$dom->load($xmlFile);
if ($dom->validate()) {
echo "The XML file is valid according to the DTD.";
} else {
echo "The XML file is NOT valid according to the DTD.";
}
?>
Output

3) Create a JSON object representing a book with the following


properties: title, author, publishedYear, and genres (an array of strings)
book.json
{
"Title": "Murder on the Orient Express",
"Author": "Agatha Christie",
"PublishedYear": 1934,
"Genres": ["Romance", "Contemporary Fiction", "Romantic Comedy"]
}
4) Create a JSON object that includes all the following data types: string,
number, boolean, null, object, and array.
Code
{
"name": "Mohammed Akib",
"age": 18,
"isStudent": true,
"address": null,
"contact": {
"email": "[email protected]",
"phone": "9136732965"
},
"Team": ["RCB", "Real Madrid", "MI"]
}
Output

5) Create a web page to handle asynchronous requests using AJAX on


Mouseover and button click.
Html Code
<!DOCTYPE html>
<html>
<head>
<title>AJAX Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h1>AJAX Example</h1>
<div id="mouse-over-div"> <h4>Akib Siddiqui<br>KCFYCS67</h4> Mouse over me!</div>
<button id="click-button">Click me!</button>
<div id="ajax-response">AJAX response will appear here...</div>
<script>
$(document).ready(function() {
$("#mouse-over-div").mouseover(function() {
$.ajax({
type: "GET",
url: "hello.php",
data: { action: "mouseover" },
success: function(response) {
$("#ajax-response").html(response);
} });});
$("#click-button").click(function() {
$.ajax({
type: "POST",
url: "hello.php",
data: { action: "button_click" },
success: function(response) {
$("#ajax-response").html(response);}});}); });
</script> </body></html>
Output

Hello.php code
<?php
if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
if ($action == 'mouseover') {
echo "Mouse over event detected!";
} elseif ($action == 'button_click') {
echo "Button click event detected!";
}}
?>
6) Create a webpage that allows users to hide, show, fade in, fade out, and
toggle the visibility of a &lt;div&gt; element containing text
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Visibility Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<style>
#demo {
width: 200px;
padding: 20px;
border: 1px solid #646765;
background-color: #E9967A; }
</style>

<body>
<h4>Mohammed Akib KCFYCS67<br></h4>
<button id="hideBtn">Hide</button>
<button id="showBtn">Show</button>
<button id="fadeOutBtn">Fade Out</button>
<button id="fadeInBtn">Fade In</button>
<button id="toggleBtn">Toggle</button>
<div id="demo">
<h2>This is a animation box!</h2>
<p>This is some text inside the paragraph. You can hide, show, fade in, fade out, and toggle the
visibility of this content!</p>
</div>

<script>
$(document).ready(function(){
// Hide the entire div when the Hide button is clicked
$("#hideBtn").click(function(){
$("#demo").hide();
});
$("#showBtn").click(function(){
$("#demo").show();
});
$("#fadeOutBtn").click(function(){
$("#demo").fadeOut();
});
$("#fadeInBtn").click(function(){
$("#demo").fadeIn();
});
$("#toggleBtn").click(function(){
$("#demo").toggle();
});
});
</script>
</body>
</html>
Output
7) Create a webpage with a &lt;div&gt; element and buttons to slide down,
slide up, and animate its size while fading in and out.
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Slide and Animate Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h4>Mohammed Akib KCFYCS67 <br></h4>
<button id="slideDownBtn">Slide Down</button>
<button id="slideUpBtn">Slide Up</button>
<button id="fadeOutBtn">Fade Out</button>
<button id="fadeInBtn">Fade In</button>
<button id="animateSizeBtn">Animate Size</button>

<div id="demoBox" style="width: 300px; height: 100px; background-color: #4CAF50; color:


white; display: none;">
<h2>This is a demo box!</h2>
<p>This content will slide, fade, and animate.</p>
</div>
<script>
$(document).ready(function(){
// Slide down the demo box when the Slide Down button is clicked
$("#slideDownBtn").click(function(){
$("#demoBox").slideDown();
});

// Slide up the demo box when the Slide Up button is clicked


$("#slideUpBtn").click(function(){
$("#demoBox").slideUp();
});

// Fade out the demo box when the Fade Out button is clicked
$("#fadeOutBtn").click(function(){
$("#demoBox").fadeOut();
});

// Fade in the demo box when the Fade In button is clicked


$("#fadeInBtn").click(function(){
$("#demoBox").fadeIn();
});

// Animate the size of the demo box when the Animate Size button is clicked
$("#animateSizeBtn").click(function(){
$("#demoBox").animate({
width: "500px",
height: "200px"
}, 1000); // Animate size over 1 second
});
});
</script>
</body>
</html>
Output

8) Write PHP scripts for


a. Retrieving data from HTML forms
b. Performing certain mathematical operations such as calculating
factorial / finding Fibonacci Series / Displaying Prime Numbers in a
given range / Evaluating Expressions / Calculating reverse of a
number
c. Working with Arrays
d. Working with Files (Reading / Writing)

1. Calculating factorial
<?php
function factorial($n) {
$result = 1;
for ($i = 1; $i <= $n; $i++) {
$result *= $i;
}
return $result;}
echo "Mohammed Akib KCFYCS67<br>";
echo "Factorial of 20: " . factorial(5) . "<br>";
?>
Output

2. Finding Fibonacci series


<?php
function fibonacci($n) {
$first = 0;
$second = 1;
echo $first . " " . $second . " ";

for ($i = 2; $i < $n; $i++) {


$next = $first + $second;
echo $next . " ";
$first = $second;
$second = $next;
}
}
$terms = 8;
echo "Mohammed Akib KCFYCS67<br>";
fibonacci($terms);
?>
Output

3. Displaying Prime numbers in a given range


Code
<?php
function primenum($start, $end) {
for ($num = $start; $num <= $end; $num++) {
$isPrime = true;
for ($i = 2; $i <= $num / 2; $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime && $num > 1) {
echo $num . " ";
}
}
}
$start = 1;
$end = 60;
echo "Mohammed Akib KCFYCS67<br>";
primenum($start, $end);
?>
Output

4. Evaluating Expressions
Code
<?php
function evaluateExpression($expression) {
eval("\$result = $expression;");
return $result;
}
$expression = "3 + 10 * 6";
echo "Mohammed Akib KCFYCS67<br>";
echo "The result of '$expression' is: " . evaluateExpression($expression);
?>
Output

5. Calculating Reverse of a number


Code
<?php
$num = 916777;
$reversedNum = '';

while ($num > 0) {


$remainder = $num % 10;
$reversedNum .= $remainder;
$num = (int)($num / 10);
}

echo "Mohammed Akib: KCFYCS67<br>";


echo "Original Number: 916777<br>";
echo "Reversed Number: $reversedNum<br>";
?>
Output

c.working with arrays

d.Working with Files (Reading / Writing)

1. Writing to a File in PHP.


Code
<?php
echo"Mohammed Akib KCFYCS67<br>";
$file = fopen("example.txt", "w");
fwrite($file, "Hii everyone I am Mohammed Akib ");
fclose($file);
echo "Data written to file successfully.";
?>
Output
1.Reading from a File in PHP
Code
<?php
$filename = "example.txt";
if (file_exists($filename))
{
$file = fopen($filename, "r");
$content = fread($file, filesize($filename));
fclose($file);
echo "File content: ".$content;
}
else
{
echo "File does not exist.";
}
?>
Output

9) Write PHP scripts for (using MySQLi and PHP Data Objects (PDO))-
Working with Databases (Storing Records / Reprieving Records and Display
them)
a. Storing and Retrieving Cookies
b. Storing and Retrieving Sessions
MySQLi
STORING RECORDS:-
<?php
$servername="localhost";
$username="root";
$password="";
$database="KCFYCS73";
$con=mysqli_connect($servername,$username,$password,$database);
$sql="INSERT INTO Students(firstname,lastname,email)
VALUES('Ananya','Thakur','[email protected]')";
if(mysqli_query($con,$sql)){
echo "New Record added successfully";
}else{
echo "Error".mysqli_error($con);
}
mysqli_close($con);
?>
OUTPUT:-

RETRIEVING RECORDS AND DISPLAY THEM:-


<?php
$conn = mysqli_connect("localhost", "root", "", "KCFYCS73");
$sql = "SELECT id, firstname, lastname, email FROM Students";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
OUTPUT:-

PHP Data Objects (PDO)


STORING RECORDS:-
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "KCFYCS73";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO Students(firstname, lastname, email)
VALUES ('Ram', 'Sharma', '[email protected]')";
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
OUTPUT:-

RETRIEVING RECORDS AND DISPLAY THEM:-


<?php
try {
$conn = new PDO("mysql:host=localhost;dbname=KCFYCS73", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM Students");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
echo "ID: " . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . " - Email: " .
$row["email"] . "<br>";}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
OUTPUT:-

a. Storing and Retrieving Cookies


Storing Cookies
CODE:-
<?php
echo"Anushka<br>Thakur<br>";
setcookie("username","Anushka",time()+3600,"/");
?>
Retrieving Cookies
CODE:-
<?php
echo"Anushka Thakur<br>KCFYCS73<br>";
if(isset($_COOKIE["username"])){
echo"Welcome, ".$_COOKIE["username"];
}else{echo"Cookie is not set!";}
?>
OUTPUT:-

b. Storing Sessions
CODE:-
<?php
echo"Anushka Thakur<br>KCFYCS73<br>";
session_start();
$_SESSION["username"]="Anushka";
echo"Session is set!";
?>
OUTPUT:-

Retrieve a Session
CODE:-
<?php
echo"Anushka Thakur<br>KCFYCS73<br>";
session_start();
if(isset($_SESSION["username"])){
echo"Welcome, ".$_SESSION["username"];
}else{echo"No session found!";}
?>
OUTPUT:-

You might also like