Download => TYBCS new HTML & Bootstrap Assignments with solution :
Updated Practical Slips Solutions Sem-II
Slip 1: Write a PHP script to keep track of number of times the web page has been accessed (Use Session
Tracking).
Solution:
<?php
session_start();
if(isset($_SESSION['page_views'])){
$_SESSION['page_views']++;
} else {
$_SESSION['page_views'] = 1;
}
echo "You have visited this page " . $_SESSION['page_views'] . " times.";
?>
=========================================================================
Slip 2: Write a PHP script to change the preferences of your web page like font style, font size, font color,
background color using cookie. Display selected setting on next web page and actual implementation
(with new settings) on third page (Use Cookies).
Solution:
HTML file :
<html>
<body>
<form action="slip2.php" method="get">
<center>
<b>Select font style :</b><input type=text name=s1> <br>
<b>Enter font size : </b><input type=text name=s><br>
<b>Enter font color : </b><input type=text name=c><br>
<b>Enter background color :</b> <input type=text name=b><br>
<input type=submit value="Next">
</center>
</form>
</body>
</html>
PHP file : slip2.php
<?php
echo "style is ".$_GET['s1']."<br>color is ".$_GET['c']."<br>Background color is ".$_GET['b']."<br>size is ".$_GET['s'];
setcookie("set1",$_GET['s1'],time()+3600);
setcookie("set2",$_GET['c'],time()+3600);
setcookie("set3",$_GET['b'],time()+3600);
setcookie("set4",$_GET['s'],time()+3600);
?>
<html>
<body>
<form action="slip2_1.php">
<input type=submit value=OK>
</form>
</body>
</html>
PHP file : slip2_1.php
<?php
$style = $_COOKIE['set1'];
$color = $_COOKIE['set2'];
$size = $_COOKIE['set4'];
$b_color = $_COOKIE['set3'];
$msg = "Perfect eLearn Institute | Umesh Ahire - 8788100181";
echo "<body bgcolor=$b_color>";
echo "<font color=$color size=$size>$msg";
echo "</font></body>";
?>
=========================================================================
Slip 3: Write a PHP script to accept username and password. If in the first three chances, username and password entered is correct then display second form with “Welcome message” otherwise display error message. [Use Session]
Solution:
HTML file :
<html>
<head>
<script>
function getans()
{
st1=document.getElementById('txtname').value;
st2=document.getElementById('txtpass').value;
ob=new XMLHttpRequest();
ob.onreadystatechange=function()
{
if(ob.readyState==4 && ob.status==200)
{
if(ob.responseText==3)
{
alert("sorry you lost the chances to login");
location="error.html";
}
else if(ob.responseText=="correct")
{
alert("you entered correct details");
}
else alert(ob.responseText);
}
}
ob.open("GET","Slip3.php?n="+st1+"&p="+st2);
ob.send();
}
</script>
</head>
<body>
<input type=text id=txtname placeholder="username"></br>
<input type=password id=txtpass placeholder="password"></br>
<input type="button" onclick="getans()" value="Login">
</body>
</html>
HTML file : error.html
<html>
<body>
<h1>YOu lossed the chances of login</h1>
</body>
</html>
PHP file : Slip3.php
<?php
session_start();
$nm=$_GET['n'];
$ps=$_GET['p'];
if($nm==$ps)
{
echo "correct";
}
else if(isset($_SESSION['cnt']))
{
$x=$_SESSION['cnt'];
$x=$x+1;
$_SESSION['cnt']=$x;
echo $_SESSION['cnt'];
if($_SESSION['cnt']>=3)
$_SESSION['cnt']=1;
}
else
{
$_SESSION['cnt']=1;
echo "1";
}
?>
=========================================================================
Slip 4:Write a PHP script to accept Employee details (Eno, Ename, Address) on first page. On second page accept earning (Basic, DA, HRA). On third page print Employee information (Eno, Ename, Address, Basic, DA, HRA, Total) [ Use Session]
Solution:
HTML file :
<html>
<body>
<form action="Slip4.php" method="get">
<center> <h2>Enter Enployee Details :</h2> <br>
<table>
<tr> <td><b>Emp no :</b></td> <td><input type=text name=eno></td> </tr>
<tr> <td><b> Name :</b></td> <td><input type=text name=enm></td> </tr>
<tr> <td><b>Address :</b></td> <td><input type=text name=eadd></td> </tr>
</table>
<br> <input type=submit value=Show name=submit>
</center>
</form>
</body>
</html>
PHP file : Slip4.php
<?php
session_start();
$eno = $_GET['eno'];
$enm = $_GET['enm'];
$eadd = $_GET['eadd'];
$_SESSION['eno'] = $eno;
$_SESSION['enm'] = $enm;
$_SESSION['eadd'] = $eadd;
?>
<html>
<body>
<form action="Slip4_1.php" method="post">
<center>
<h2>Enter Earnings of Employee:</h2>
<table>
<tr><td>Basic : </td><td><input type="text" name="e1"></td><tr>
<tr><td>DA : </td><td><input type="text" name="e2"></td></tr>
<tr><td>HRA : </td><td><input type="text" name="e3"></td></tr>
<tr><td></td><td><input type="submit" value=Next></td></tr>
</table>
</center>
</form>
</body>
</html>
PHP file : slip4_1.php
<?php
session_start();
$e1 = $_POST['e1'];
$e2 = $_POST['e2'];
$e3= $_POST['e3'];
echo "<h3>Employee Details</h3> ";
echo "Name : ".$_SESSION['eno']."<br>";
echo "Address : ".$_SESSION['enm']."<br>";
echo "Class : ".$_SESSION['eadd']."<br><br>";
echo "basic : ".$e1."<br>";
echo "DA : ".$e2."<br>";
echo "HRA : ".$e3."<br>";
$total = $e1+$e2+$e3;
echo "<h2>Total Of Earnings Is : ".$total."</h2>";
?>
=========================================================================
Slip 5:Create XML file named “Item.xml”with item-name, item-rate, item quantity Store the details of 5 Items of different Types
Solution:
XML file: Item.xml
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<name>Apple</name>
<rate>1.99</rate>
<quantity>10</quantity>
</item>
<item>
<name>Orange</name>
<rate>0.99</rate>
<quantity>15</quantity>
</item>
<item>
<name>Banana</name>
<rate>0.79</rate>
<quantity>20</quantity>
</item>
<item>
<name>Tomato</name>
<rate>0.49</rate>
<quantity>25</quantity>
</item>
<item>
<name>Cucumber</name>
<rate>0.69</rate>
<quantity>30</quantity>
</item>
</items>
=========================================================================
Slip 6: Write PHP script to read “book.xml” file into simpleXML object. Display attributes and elements. ( simplexml_load_file() function )
Solution:
XML File: Book.xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1">
<title>The Catcher in the Rye</title>
<author>J.D. Salinger</author>
<year>1951</year>
</book>
<book id="2">
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
</book>
</library>
PHP File: Book.php
<?php
// Load the XML file into a SimpleXML object
$xml = simplexml_load_file("book.xml");
// Display the attributes of the first book element
echo "Attributes:\n";
foreach ($xml->book[0]->attributes() as $name => $value) {
echo "$name: $value\n";
}
// Display the elements of the first book element
echo "\nElements:\n";
foreach ($xml->book[0]->children() as $child) {
echo $child->getName() . ": " . $child . "\n";
}
?>
=========================================================================
Slip 7: Write a PHP script to read “Movie.xml” file and print all MovieTitle and ActorName of file using DOMDocument Parser. “Movie.xml” file should contain following information with at least 5 records with values. MovieInfoMovieNo, MovieTitle, ActorName ,ReleaseYear
Solution:
XML File: Movie.xml
<?xml version="1.0"?>
<Movies>
<MovieInfo>
<MovieNo>1</MovieNo>
<MovieTitle>The Shawshank Redemption</MovieTitle>
<ActorName>Tim Robbins</ActorName>
<ReleaseYear>1994</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>2</MovieNo>
<MovieTitle>The Godfather</MovieTitle>
<ActorName>Marlon Brando</ActorName>
<ReleaseYear>1972</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>3</MovieNo>
<MovieTitle>The Dark Knight</MovieTitle>
<ActorName>Christian Bale</ActorName>
<ReleaseYear>2008</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>4</MovieNo>
<MovieTitle>Schindler's List</MovieTitle>
<ActorName>Liam Neeson</ActorName>
<ReleaseYear>1993</ReleaseYear>
</MovieInfo>
<MovieInfo>
<MovieNo>5</MovieNo>
<MovieTitle>Pulp Fiction</MovieTitle>
<ActorName>John Travolta</ActorName>
<ReleaseYear>1994</ReleaseYear>
</MovieInfo>
</Movies>
PHP File: Movie.php
<?php
// Load the XML file
$xml = new DOMDocument();
$xml->load('Movie.xml');
// Get all the <MovieInfo> elements
$movieInfos = $xml->getElementsByTagName('MovieInfo');
// Loop through each <MovieInfo> element and print the MovieTitle and ActorName
foreach ($movieInfos as $movieInfo) {
$movieTitle = $movieInfo->getElementsByTagName('MovieTitle')->item(0)->nodeValue;
$actorName = $movieInfo->getElementsByTagName('ActorName')->item(0)->nodeValue;
echo "Movie Title: $movieTitle\n";
echo "Actor Name: $actorName\n\n";
}
?>
=========================================================================
Slip 8: Write a JavaScript to display message ‘Exams are near, have you started preparing for?’ (usealert box ) and Accept any two numbers from user and display addition of two number .(Use Prompt and confirm box)
Solution:
// Display message using alert box
alert("Exams are near, have you started preparing for?");
// Accept two numbers from user using prompt box
let num1 = prompt("Enter the first number:");
let num2 = prompt("Enter the second number:");
// Convert the input strings to numbers
num1 = parseFloat(num1);
num2 = parseFloat(num2);
// Check if both inputs are valid numbers
if (isNaN(num1) || isNaN(num2)) {
// Display error message using confirm box
confirm("Invalid input. Please enter numbers only.");
} else {
// Compute the sum and display using confirm box
const sum = num1 + num2;
confirm(`The sum of ${num1} and ${num2} is ${sum}.`);
}
=========================================================================
Slip 9 : Write a JavaScript function to validate username and password for a membership form.
Solution:
function validateForm() {
const username = document.forms["membershipForm"]["username"].value;
const password = document.forms["membershipForm"]["password"].value;
// check if username is empty
if (username === "") {
alert("Please enter a username.");
return false;
}
// check if password is empty
if (password === "") {
alert("Please enter a password.");
return false;
}
// check if password is at least 8 characters long
if (password.length < 8) {
alert("Password must be at least 8 characters long.");
return false;
}
// username and password are valid
return true;
}
=========================================================================
Slip 10: Create a HTML fileto insert text before and after a Paragraph using jQuery.
[Hint : Use before( ) and after( )]
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Insert Text Before and After Paragraph Using jQuery</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<p id="my-paragraph">This is my paragraph.</p>
<script>
// insert text before the paragraph
$("#my-paragraph").before("<p>Here's some text before the paragraph.</p>");
// insert text after the paragraph
$("#my-paragraph").after("<p>And here's some text after the paragraph.</p>");
</script>
</body>
</html>
=========================================================================
Slip 11: Write a Javascript program to accept name of student, change font color to red, font size to 18 if student name is present otherwise on clicking on empty text box display image which changes its size (Use onblur, onload, onmousehover, onmouseclick, onmouseup)
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Student Name</title>
<script>
function changeStyle() {
var name = document.getElementById("name");
if (name.value != "") {
name.style.color = "red";
name.style.fontSize = "18px";
} else {
var image = document.getElementById("image");
image.style.display = "block";
}
}
function changeSize() {
var image = document.getElementById("image");
image.style.width = "200px";
image.style.height = "200px";
}
function resetSize() {
var image = document.getElementById("image");
image.style.width = "100px";
image.style.height = "100px";
}
</script>
</head>
<body>
<h1>Student Name</h1>
<input type="text" id="name" onblur="changeStyle()">
<br><br>
<img id="image" src="https://picsum.photos/id/237/100/100" onclick="changeSize()" onmouseup="resetSize()" onmouseover="changeSize()" onmouseout="resetSize()" style="display:none;">
</body>
</html>
==============================================================
Slip 12: Write AJAX program to read contact.dat file and print the contents of the file in a tabular format when the user clicks on print button. Contact.dat file should contain srno, name, residence number, mobile number, Address. [Enter at least 3 record in contact.dat file]
Solution:
HTML file :
<html>
<head>
<style>
span
{
font-size: 25px;
}
table
{
color: blueviolet; ;
}
</style>
<script type="text/javascript" >
function print()
{
var ob=false;
ob=new XMLHttpRequest();
ob.open("GET","slip12.php?");//emailid="+eid);
ob.send();
ob.onreadystatechange=function()
{
if(ob.readyState==4 && ob.status==200)
{
document.getElementById("i").innerHTML=ob.responseText;
}
}
}
</script>
</head>
<body>
<center>
<h3>Display the contents of a contact.dat file </h3>
<br><input type="button" value=Print onclick="print()" >
<span id="i"></span>
</center>
</body>
</html>
Dat file : contact.dat
1 Isha 65768798 98765432 Daughter
2 Megha 65235689 87654329 Mother
PHP File: Slip12.php
<?php
$fp = fopen('contact.dat','r');
echo "<table border=1>";
echo "<tr><th>Sr. No.</th><th>Name</th><th>Residence No.</th><th>Mob. no.</th><th>Relation</th></tr>";
while($row = fscanf($fp,"%s %s %s %s %s"))
{
echo "<tr>";
foreach($row as $r)
{
echo "<td>$r</td>";
}
echo "</tr>";
}
echo "</table>";
fclose($fp);
?>
=========================================================================
Slip13: Write AJAX program where the user is requested to write his or her name in a text box, and the server keeps sending back responses while the user is typing. If the user name is not entered then the message displayed will be, “Stranger, please tell me your name!”. If the name is Rohit, Virat, Dhoni, Ashwin or Harbhajan , the server responds with “Hello, master !”. If the name is anything else, the message will be “, I don’t know you!”
Solution:
HTML File: server.html
<!DOCTYPE html>
<html>
<head>
<title>AJAX Example</title>
<script>
function sendRequest() {
var name = document.getElementById("name").value;
var message = document.getElementById("message");
if (name == "") {
message.innerHTML = "Stranger, please tell me your name!";
} else {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText == "Hello, master !") {
message.innerHTML = "Hello, master !";
} else {
message.innerHTML = name + ", I don't know you!";
}
}
};
xhr.open("GET", "server.php?name=" + name, true);
xhr.send();
}
}
</script>
</head>
<body>
<h1>AJAX Example</h1>
<input type="text" id="name" onkeyup="sendRequest()">
<p id="message"></p>
</body>
</html>
PHP File: server.php
<?php
$name = $_GET["name"];
if ($name == "") {
echo "";
} else if ($name == "Rohit" || $name == "Virat" || $name == "Dhoni" || $name == "Ashwin" || $name == "Harbhajan") {
echo "Hello, master !";
} else {
echo "";
}
?>
=========================================================================