web development practical file
web development practical file
PRACTICAL FILE
ON
Web Development
24CSC402SE01
Sem-02
Session: 2024-27
DPG STM
S.no. Pg.no.
Topics
Create an XML document to store information about a collection of
1 books, including elements for author, publication year, and genre.
Write a simple html page that uses JavaScript to load and display the
XML data.
Create an XML file and an XSLT stylesheet to transform the
2 XML data into an html table. Use the transformation Node or
XSLTProcessor methods to apply the transformation and
display the result in a web page.
Write an HTML page with an script that loads an XML file containing a
3 list of employees. Parse the XML data with JavaScript and display the
employee names and departments in the list of the web page.
Write an HTML page with an script that loads an XML file containing a list of
4 employees. Parse the XML data with JavaScript and display the employee
names and departments in the list of the web page.
Write a Ajax Script to get player details from xml file when user select
5 player name game. Also create xml file to store details of
player(name, country, wickets and runs).
Write a AJAX request to URL that does not exist. Implement error
6 handling to display a user friendly message when the request fails.
Write a jQuery code to select values from a JSONobject. Also
7 Write a jQuery code to remove all CSS classes from an
Applications.
Create a php Script that defines variable of different data
8 types (string, integer, float, Boolean, array). Output the values
and types of these variables using echo and var_dump.
Develop an HTML form to upload a file. Write a php
9 script to handle the file upload, save the file on the
server, and display a message indicating the file upload
status.
Create a MySQL database and table to store user information
10 (name, email). Write a PHP Script to connect to the database
and insert a new user record. Display a success message upon
successful insertion.
<library>
<book>
<author>J.D. Salinger</author>
<year>1951</year>
<genre>Fiction</genre>
</book>
<book>
<title>1984</title>
<author>George Orwell</author>
<year>1949</year>
<genre>Dystopian</genre>
</book>
<book>
<author>Harper Lee</author>
<year>1960</year>
<genre>Fiction</genre>
</book>
<book>
<author>Aldous Huxley</author>
<year>1932</year>
<genre>Dystopian</genre>
</book>
</library>
HTML Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book Collection</title>
</head>
<body>
<h1>Book Collection</h1>
<div id="bookList"></div>
<script>
<library>
<book>
<title>The guide</title>
<author>R K Narayan</author>
<year>1906</year>
<genre>writer</genre>
</book>
<book>
<author>Ruskin Bond</author>
<year>1961</year>
<genre>Indian Litrature</genre>
</book>
<book>
<title>The Namesake</title>
<author>Jhumpa Lahiri</author>
<year>1967</year>
<genre>Litery Fiction</genre>
</book>
<book>
<author>Aldous Huxley</author>
<year>1932</year>
<genre>Dystopian</genre>
</book>
</library>`;
function loadXML() {
output += "</ul>";
document.getElementById("bookList").innerHTML = output;
// Call the function to load the XML when the page loads
window.onload = loadXML;
</script>
</body>
</html>
Output:
Q.2 Create an XML file and an XSLT stylesheet to transform the XML
data into an html table. Use the transformation Node or XSLTProcessor
methods to apply the transformation and display the result in a web
page.
XML Syntax:
<student>
<name>John Doe</name>
<age>20</age>
<grade>A</grade>
</student>
<student>
<name>Jane Smith</name>
<age>22</age>
<grade>B</grade>
</student>
<student>
<name>Mark Johnson</name>
<age>21</age>
<grade>A</grade>
</student>
</students><!-- XSLT Stylesheet (style.xsl) --><?xml version="1.0"
encoding="UTF-8"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template
match="/"> <html> <head> <title>Student List</title> </head> <body>
<h2>Student Information</h2> <table border="1"> <tr> <th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr> <xsl:for-each
select="students/student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of
select="grade"/></td> </tr>
</xsl:for-each> </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML Syntax:
<!-- HTML File to display transformation (index.html) --><!DOCTYPE
html><html>
<head>
<title>XML to HTML Transformation</title>
</head>
<body>
<script>
if (window.ActiveXObject || "ActiveXObject" in window) { // For IE
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("students.xml");
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("style.xsl");
document.write(xml.transformNode(xsl));
} else if (document.implementation &&
document.implementation.createDocument) { // For modern browsers
var xsltProcessor = new XSLTProcessor();
var xmlDoc = new DOMParser().parseFromString(<?xml
version='1.0'?><students><student><name>John
Doe</name><age>20</age><grade>A</grade></student><student><nam
e>Jane
Smith</name><age>22</age><grade>B</grade></student><student><na
me>Mark
Johnson</name><age>21</age><grade>A</grade></student></students>
, 'text/xml');
var xslDoc = new DOMParser().parseFromString(<?xml
version='1.0'?> <xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template
match='/'> <html><head><title>Student
List</title></head><body><h2>Student Information</h2><table
border='1'><tr><th>Name</th><th>Age</th><th>Grade</th></tr><xsl:foreach select='students/student'><tr><td><xsl:value-of
select='name'/></td><td><xsl:value-of select='age'/></td><td><xsl:valueof
select='grade'/></td></tr></xsl:foreach></table></body></html></xsl:template></xsl:stylesheet>,
'text/xml');
xsltProcessor.importStylesheet(xslDoc);
var resultDocument = xsltProcessor.transformToFragment(xmlDoc,
document);
document.body.appendChild(resultDocument);
}
</script>
</body>
</html>
Output:
Q.3 Write an HTML page with an script that loads an XML file
containing a list of employees. Parse the XML data with JavaScript and
display the employee names and departments in the list of the web
page.
Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Employee List</title>
<style>
body {
padding: 20px;
h1 {
text-align: center;
#employeeList {
margin-top: 20px;
.employee {
margin: 10px 0;
padding: 10px;
border-radius: 5px;
}
.employee .name {
font-weight: bold;
.employee .department {
font-style: italic;
</style>
</head>
<body>
<h1>Employee List</h1>
<div id="employeeList"></div>
<script>
// Simulated XML data (In real scenario, you would load it from a file or server)
<employees>
<employee>
<name>John Doe</name>
<department>HR</department>
</employee>
<employee>
<name>Jane Smith</name>
<department>IT</department>
</employee>
<employee>
<name>George Brown</name>
<department>Finance</department>
</employee>
<employee>
<name>Lisa White</name>
<department>Marketing</department>
</employee>
</employees>`;
function displayEmployees(xmlString) {
employeeList.innerHTML = '';
employeeDiv.classList.add("employee");
nameElement.classList.add("name");
nameElement.textContent = name;
departmentElement.classList.add("department");
departmentElement.textContent = department;
employeeDiv.appendChild(nameElement);
employeeDiv.appendChild(departmentElement);
employeeList.appendChild(employeeDiv);
displayEmployees(xmlData);
</script>
</body>
</html>
Output:
Q.4 Develop a form that allows users to submit their contact
information (name, email, message). Use AJAX to send the form data
to a server-side Script (e.g., a PHP Script) that echoes the received
data. Display the server’s response on the web page.
Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<script>
function sendForm(event) {
event.preventDefault();
fetch("process.php", {
method: "POST",
body: formData
})
</script>
</head>
<body>
<h2>Contact Us</h2>
<button type="submit">Send</button>
</form>
<div id="response"></div>
</body>
</html>
Output:
Q.5 Write a Ajax Script to get player details from xml file when user
select player name game. Also create xml file to store details of
player(name, country, wickets and runs).
Xml Syntax:
<?xml version="1.0" encoding="UTF-8"?>
<players>
<player>
<name>Virat Kohli</name>
<country>India</country>
<wickets>0</wickets>
<runs>12000</runs>
</player>
<player>
<name>Steve Smith</name>
<country>Australia</country>
<wickets>0</wickets>
<runs>8000</runs>
</player>
<player>
<name>Ben Stokes</name>
<country>England</country>
<wickets>150</wickets>
<runs>5000</runs>
</player>
<player>
<name>Shakib Al Hasan</name>
<country>Bangladesh</country>
<wickets>250</wickets>
<runs>6000</runs>
</player>
</players>
Output:
XML+Ajax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Player Details</title>
<style>
body {
padding: 20px;
h1 {
text-align: center;
#playerDetails {
margin-top: 20px;
padding: 10px;
border-radius: 5px;
</style>
</head>
<body>
<h1>Player Details</h1>
<select id="playerSelect">
</select>
<div id="playerDetails"></div>
<script>
function loadPlayerDetails(playerName) {
if (!playerName) {
document.getElementById('playerDetails').innerHTML = '';
return;
xhr.onreadystatechange = function() {
playerInfo = `
<h2>Player: ${name}</h2>
<p><strong>Country:</strong> ${country}</p>
<p><strong>Wickets:</strong> ${wickets}</p>
<p><strong>Runs:</strong> ${runs}</p>
`;
break;
document.getElementById('playerDetails').innerHTML = playerInfo;
};
xhr.send();
document.getElementById('playerSelect').addEventListener('change', function() {
loadPlayerDetails(selectedPlayer);
});
</script>
</body>
</html>
Output:
Q.6 Write a AJAX request to URL that does not exist. Implement error
handling to display a user friendly message when the request fails.
Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
function fetchData() {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
document.getElementById("result").innerHTML = xhr.responseText;
} else {
document.getElementById("result").innerHTML =
};
xhr.send();
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
Output:
Q.7 Write a jQuery code to select values from a JSONobject. Also Write
a jQuery code to remove all CSS classes from an Applications.
HTML Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
$(document).ready(function() {
var jsonObject = {
"name": "John",
"age": 30,
};
});
</script>
</body>
</html>
Output:
Q.8 Create a php Script that defines variable of different data types
(string, integer, float, Boolean, array). Output the values and types of
these variables using echo and var_dump.
Syntax:
<?php
// String
// Integer
$age = 30;
// Float
$price = 19.99;
// Boolean
$is_active = true;
// Array
var_dump($name);
echo "<br><br>";
var_dump($age);
echo "<br><br>";
var_dump($price);
echo "<br><br>";
var_dump($is_active);
echo "<br><br>";
var_dump($colors);
?>
Output:
Q.9 Develop an HTML form to upload a file. Write a php script to
handle the file upload, save the file on the server, and display a
message indicating the file upload status.
Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>Upload a File</h2>
<br><br>
</form>
</body>
</html>
Output:
Q.10 Create a MySQL database and table to store user information
(name, email). Write a PHP Script to connect to the database and
insert a new user record. Display a success message upon successful
insertion.
Syntax:
-- Create the database
USE user_database;
);
Output:
Q.11 Create a web page for travel agency using PHP.
Syntax:
<?php
$successMessage = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate name
if (empty($_POST["name"])) {
} else {
$name = sanitize_input($_POST["name"]);
// Validate email
if (empty($_POST["email"])) {
} else {
$email = sanitize_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Validate destination
if (empty($_POST["destination"])) {
} else {
$destination = sanitize_input($_POST["destination"]);
$successMessage = "Thank you, $name! We have received your request to travel to $destination. We will contact you at
$email.";
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Travel Agency</title>
<style>
body {
background-color: #f0f8ff;
margin: 0;
padding: 0;
header {
background-color: #4CAF50;
color: white;
text-align: center;
padding: 10px 0;
.container {
width: 60%;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
form {
display: flex;
flex-direction: column;
label {
margin-top: 10px;
font-weight: bold;
padding: 8px;
margin: 5px 0;
border-radius: 4px;
input[type="submit"] {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
.error {
color: red;
.success {
color: green;
font-weight: bold;
</style>
</head>
<body>
<header>
</header>
<div class="container">
<?php
if ($successMessage != "") {
?>
<label for="email">Email:</label>
<option value="New York" <?php if ($destination == "New York") echo "selected";?>>New York</option>
</select>
</form>
</div>
</body>
</html>
Output: