Code
Code
PRACTICAL NO. -1
Description: In this program, Javascript events are demonstrated. The event handlers are
used to detect the events and perform specific tasks. The name of event handlers always
begins with on. An event can be assigned directly to the HTML element using the event
handler as an attribute.
1.1 Click Event: In this program, click event is used on a button, when the user clicks this
button then an alert box will pop up.
Source Code:
<html>
<head>
<title>
Click Event
</title>
<script>
function msg()
</script>
</head>
<body>
<h2>Web Technology</h2>
<form>
</form>
</body>
</html>
Output:
Click Event:
1.2 Mouseover Event: In this program, mouseover event is used on a paragraph, when the
user brings the mouse pointer over the paragraph then an alert box will pop up.
Source Code:
<html>
<head>
<title>
Mouseover Event
</title>
</head>
<body>
<script>
function mouseoverevent()
</script>
</body>
</html>
Output:
Mouseover Event:
1.3 Focus Event: In this program, focus event is used on a textbox, when the user focuses on
this textbox that is clicks the textbox to enter something then the colour of the textbox will
change.
Source Code:
<html>
<head>
<title>
Focus Event
</title>
</head>
<body>
<script>
function focusevent()
document.getElementById("i1").style.background="grey";
</script>
</body>
</html>
Output:
Focus Event:
1.4 Keydown Event: In this program, keydown event is used on a textbox, when the user
presses any key to enter something in this textbox then an alert box will pop up.
Source Code:
<html>
<head>
<title>
Keydown Event
</title>
</head>
<body>
<script>
function keydownevent()
document.getElementById("i1");
alert("Pressed a key");
</script>
</body>
</html>
Output:
Keydown Event:
1.5 Load Event: In this program, load event is used with body element, when the user will
load this page then an alert box will pop up.
Source Code:
<html>
<head>
<title>
Load Event
</title>
</head>
<script>
</script>
</body>
</html>
Output:
PRACTICAL NO. -2
Description: In this program, we can print the table of any number entered by the user using
loop and the ‘*’ operator. The loop will operate till the desired number entered by the user,
up to which the table is to be printed. The number whose table is to be printed is then
multiplied with the numbers till that number.
Source Code:
<html>
<head>
<title>
Table
</title>
</head>
<body>
<script>
const n=prompt("Enter the number upto which you want the table");
for(i=1;i<=n;i++)
document.write("<br>");
document.write("<br>");
</script>
</body>
</html>
Output:
Table:
PRACTICAL NO. -3
Source Code:
<html>
<head>
<title>
Random Number
</title>
</head>
<body>
<script>
function random()
const r=Math.random();
</script>
</body>
</html>
Output:
PRACTICAL NO. -4
OBJECTIVE: Write a program to display today’s date and time using date() function.
Description: In this program, the Date object is used to get the current date and time. It
provides methods for retrieving and manipulating the components of date and time, such as
year, month, day, hours, minutes, seconds, and milliseconds. The Date() constructor creates a
new date object representing current date and time.
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<p id="dnt"></p>
<script>
function datentime()
document.getElementById("dnt").innerHTML=d;
</script>
</body>
</html>
Output:
PRACTICAL NO. -5
Description: In this program, the HTML form is validated using Javascript. It helps to ensure
that the data collected from the user is accurate and complete.
Source Code:
<html>
<head>
<title>
Form Validation
</title>
<style>
form
margin-left:470px;
margin-right:470px;
padding:10px;
h1
color:grey;
text-align:center;
</style>
<script>
function val()
var a=document.getElementById("name").value;
var b=document.getElementById("fname").value;
var c=document.getElementById("addrs").value;
var d=document.getElementById("eaddrs").value;
var e=document.getElementById("contact").value;
if(a==""||b==""||c==""||d==""||e=="")
return false;
else if(e.length<10||e.length>10)
return false;
return false;
else
true;
</script>
</head>
<body>
Name:<br>
Father's Name:<br>
D.O.B:<br>
Gender:<br>
Course:<br>
<select name="Course">
<option value="BCA">BCA</option>
<option value="B.Tech">B.Tech</option>
<option value="B.Sc">B.Sc</option>
<option value="B.Com">B.Com</option>
<option value="BBA">BBA</option>
<option value="BA">BA</option>
</select><br><br>
Address:<br>
Phone.No:<br>
E-mail:<br>
<button type="Submit">Submit</button>
</form>
</body>
</html>
Output:
PRACTICAL NO. -6
Description: In this program, various methods to access/find the HTML elements are
demonstrated. If we want to access any element in HTML we start with accessing the
document object.
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<p id="paragraph"></p>
<script>
function parah()
</script>
</body>
</html>
Output:
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<script>
const h=document.getElementsByClass("heading");
const p=document.getElementsByClass("parah");
document.writeln(h[0]);
document.writeln(p[0]);
document.writeln(h[1]);
document.writeln(p[1]);
document.writeln(p[2]);
</script>
</body>
</html>
Output:
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<h2>Heading 1</h2>
<h2>Heading 2</h2>
<script>
function counth()
const h=document.getElementsByTagName("h2");
function countp()
const p=document.getElementsByTagName("p");
</script>
</body>
</html>
Output:
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<script>
function counth()
const h=document.getElementsByName("heading");
function countp()
const p=document.getElementsByName("parah");
document.writeln(h[0]);
document.writeln(p[0]);
document.writeln(h[1]);
document.writeln(p[1]);
document.writeln(p[2]);
</script>
</body>
</html>
Output:
6.5 Accessing HTML Element by CSS selector: In this program, the querySelector() and
querySelectorAll() method is used to access the HTML elements by using CSS selectors.
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<h2>Heading 1</h2>
<h2>Heading 2</h2>
<script>
document.querySelector("h2").style.backgroundColor="yellow";
p=document.querySelectorAll(".parah");
for(i=0;i<p.length;i++)
p[i].style.backgroundColor="grey";
</script>
</body>
</html>
Output:
PRACTICAL NO. -7
Source Code:
<html>
<head>
<title>
</title>
</head>
<body>
<button onclick="document.getElementById('img').src='on.jpg'">On</button>
<button onclick="document.getElementById('img').src='off.jpg'">Off</button>
</body>
</html>
Output:
PRACTICAL NO. -8
Description: In this program, the echo and print are used to print Hello World in PHP. It also
shows the difference between echo and print that is echo can take multiple parameters while
print can take one argument only.
Source Code:
<html>
<body>
<?php
echo "Hello"."World<br>";
echo "Hello","World<br>";
print "Hello"."World<br>";
print "Hello";
print "World";
?>
</body>
</html>
Output:
PRACTICAL NO. -9
Description: In this program, three textboxes are used to read the numbers and then the
maximum/largest of these three numbers will be executed on clicking the submit button.
Source Code:
<html>
<body>
<form method="post">
<table>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</form>
<?php
if(isset($_POST['submit']))
$n1=$_POST['n1'];
$n2=$_POST['n2'];
$n3=$_POST['n3'];
if($n1>=$n2&&$n3<=$n1)
else if($n2>=$n1&&$n3<=$n2)
else if($n3>=$n2&&$n1<=$n3)
return 0;
?>
</body>
</html>
Output:
Description: In this program, swapping of two numbers is performed which can be done in
two ways by using a third variable and without using a third variable.
10.1 Swapping using third variable: In this program, two numbers are swapped using a
third variable. Firstly one of the value is stored in a temporary variable, then the value of
second number is assigned to the first, and then value of first number stored in the temporary
variable is assigned to the second number.
Source Code:
<html>
<body>
<form method="post">
</form>
<?php
if(isset($_POST['Swap']))
$n1=$_POST['num1'];
$n2=$_POST['num2'];
$temp=$n1;
$n1=$n2;
$n2=$temp;
?>
</body>
</html>
Output:
10.2 Swapping without using third variable: In this program, two numbers are swapped
without using a third variable with the help of addition and subtraction operators by adding
and subtracting the numbers.
Source Code:
<html>
<body>
<form method="post">
</form>
<?php
if(isset($_POST['Swap']))
$n1=$_POST['num1'];
$n2=$_POST['num2'];
$n1=$n1+$n2;
$n2=$n1-$n2;
$n1=$n1-$n2;
?>
</body>
</html>
Output:
Description: In this program PHP Functions are demonstrated which is a block of statement
that can be used repeatedly in a program. A function can have no or any number of
parameters which act as variables inside your function. A function will be executed by a call
to that function which can be done in two ways call by value and call by reference.
Call by value: In this method, the values of actual parameters are copied to the formal
parameters and any change made inside the function will not affect the original value of the
variable.
Call by reference: In this method, the address of actual parameters is passed to the function
as the formal parameters and any change made inside the function will affect the original
value of the variable.
Source Code:
<html>
<body>
<h1>PHP Function</h1>
<form method="post">
</form>
<?php
function callValue($num)
$num=$num+10;
return $num;
function callRef(&$num)
$num=$num+10;
return $num;
if(isset($_POST['call']))
$v=$_POST['call'];
if($comp==0)
$n=10;
callValue($n);
else
$n=10;
callRef($n);
return 0;
?>
</body>
</html>
Output:
Call by value:
Call by reference:
Description: In this program, various operations that can be performed on arrays are
demonstrated. An array is used to hold multiple values of similar type in a single variable.
The operations on arrays can be performed with the help of array functions which are used to
access and manipulate the elements of an array. Some of the array functions are:
Source Code:
<html>
<body>
<h1>Array Functions</h1>
<form method="post">
<input type="Submit">
</form>
<?php
if($_POST)
$val=$_POST['choice'];
switch($val)
case "display":
foreach($array1 as $value)
break;
case "sort":
sort($array1);
foreach($array1 as $value)
break;
case "duplicate":
$a=array_unique($array1);
foreach($a as $value)
break;
case "pop":
array_pop($array1);
foreach($array1 as $value)
break;
case "reverse":
$a=array_reverse($array1);
foreach($a as $value)
break;
case "search":
break;
?>
</body>
</html>
Output:
Sort:
Source Code:
<html>
<body>
<h1>Multidimensional Array</h1>
<?php
$sdetails=array(array("S.No","Name","Course"), array(1,"Jasmit","BCA"),
array(2,"Rajdeep","BBA"),array(3,"Jasleen","B.Tech"), array(4,"Harsimran","B.Com"));
for($row=0;$row<4;$row++)
for($col=0;$col<3;$col++)
echo $sdetails[$row][$col];
echo "<br>";
?>
</body>
</html>
Output:
Source Code:
<html>
<body>
<h1>String Functions</h1>
<form method="post">
Enter a string:</label>
<br><br>
</select>
<br><br>
</form>
<?php
if ($_POST)
$a = $_POST["String"];
$b = $_POST["functionSelect"];
switch ($b)
case "length":
break;
case "words":
break;
case "replace":
break;
case "reverse":
break;
case "search":
break;
case "small":
break;
case "capital":
break;
case "cmpare":
break;
case "join":
break;
case "splt":
print_r(str_split($a, 3));
break;
default:
?>
</body>
</html>
Output:
Count length:
Replace text:
Reverse string:
Search:
Convert to lowercase:
Convert to uppercase:
Join:
Split string:
Description: In this program, the methods to establish connection with database using
MySQLi are demonstrated. There are two ways of connecting the data using this method that
are object oriented procedure and procedural procedure.
15.1 Object oriented approach: In this program, we create an instance of the mysqli class
and provide all the necessary details required to establish connection.
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
// Create connection
// Check connection
if($conn->connect_error)
// closing connection
$conn->close();
?>
Output:
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
// Create connection
// Check connection
if(!$conn)
// closing connection
mysqli_close($conn);
?>
Output:
i. Create a database.
Description: In this program, the method to create a database and table using PHP are
demonstrated, along with some operations that can be performed on table stored in the
database.
16.1 Creating database: In this program, the method to create a database is demonstrated.
Firstly, we need to build a connection and then create the database using the query or through
user interface.
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
if($conn->connect_error)
die("Connection Failed:".$conn->connect_error);
if($conn->query($sql)===TRUE)
else
$conn->close();
?>
Output:
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
if(!$conn)
if(mysqli_query($conn,$sql))
else
mysqli_close($conn);
?>
Output:
16.2 Creating table: : In this program, the methods to create a table using PHP in MySQL
database are demonstrated.
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
$database="db";
if($conn->connect_error)
die("Connection Failed:".$conn->connect_error);
$sql="Create table student(stu_id int(2) PRIMARY KEY, firstname varchar(15) NOT NULL,
lastname varchar(12) NOT NULL, age int(2) NOT NULL, course varchar(10), semester
int(1) )";
if($conn->query($sql)===TRUE)
else
$conn->close();
?>
Output:
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
$database="db2";
if(!$conn)
if(mysqli_query($conn,$sql))
else
mysqli_close($conn);
?>
Output:
16.3 Inserting rows in a table: In this program, the method to insert values into a table
created in the database is demonstrated.
Source Code:
<html>
<body>
<form method="post">
</form>
<?php
$servername="localhost";
$username="root";
$password="";
$database="db";
if ($conn->connect_error)
else
if(isset($_POST['submit']))
$id = $_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$course = $_POST['course'];
$sem = $_POST['semester'];
$data=mysqli_query($conn,$query);
if($data)
?>
<script>
</script>
<?php
else
?>
<script>
</script>
<?php
$conn->close();
?>
</body>
</html>
Output:
16.4 Updating rows in a table: In this program the method to update the data in a table
using query is demonstrated.
Source Code:
<html>
<body>
<form method="post">
</form>
<?php
$servername="localhost";
$username="root";
$password="";
$database="db";
if ($conn->connect_error)
else
if(isset($_POST['submit']))
$id = $_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];
$course = $_POST['course'];
$sem = $_POST['semester'];
$data=mysqli_query($conn,$query);
if($data)
?>
<?php
else
?>
<?php
}}}
$conn->close();
?>
</body>
</html>
Output:
Previous data:
Updated data:
16.5 Deleting row in a table: In this program the method to delete a row in a table that is
data of any person is demonstrated.
Source Code:
<html>
<body>
<h3>Enter the student id of the student whose data you want to delete:</h3>
<form method="post">
</form>
<?php
$servername="localhost";
$username="root";
$password="";
$database="db";
if ($conn->connect_error)
else
if(isset($_POST['submit']))
$id = $_POST['id'];
$data=mysqli_query($conn,$query);
if($data)
?>
<script>
alert("Data Deleted!");
</script>
<?php
else
?>
<script>
</script>
<?php
$conn->close();
?>
</body>
</html>
Output:
Previous data:
OBJECTIVE: Create a form, add the data into it and submit it to the database by
connecting it to MySQL database using PHP and display all the submitted data on
another page.
Description: In this program, a form is created using HTML and the data to the form is
provided by the user which gets stored in the MySQL database when the user submits the
data. The stored data is then displayed on another page. Firstly the table and the connection is
created, then the form is created which will store all the information. Lastly, the web page
that will display the stored data is created.
Creating Table:
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
$database="db2";
if(!$conn)
if(mysqli_query($conn,$sql))
else
mysqli_close($conn);
?>
Output:
Source Code:
<?php
$servername="localhost";
$username="root";
$password="";
$database="db2";
if(!$conn)
?>
Insertion of data:
Source Code:
?>
<html>
<head>
<title>
Form
</title>
</head>
<body>
<button><a href="display.php">See</a></button>
</form>
<?php
if(isset($_POST['save']))
$fname=$_POST['firstName'];
$lname=$_POST['lastName'];
$age=$_POST['age'];
$course=$_POST['course'];
$semester=$_POST['semester'];
$contact=$_POST['contact'];
$data=mysqli_query($conn,$query);
if($data)
else
?>
</body>
</html>
Output:
Source Code:
?>
<table border="1px">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Course</th>
<th>Semester</th>
<th>Contact</th>
</tr>
<?php
$data=mysqli_query($conn, $query);
$result=mysqli_num_rows($data);
if($result)
while($row=mysqli_fetch_array($data))
?>
<tr>
<td>
<?php
echo $row['firstname'];
?>
</td>
<td>
<?php
echo $row['lastname'];
?>
</td>
<td>
<?php
echo $row['age'];
?>
</td>
<td>
<?php
echo $row['course'];
?>
</td>
<td>
<?php
echo $row['semester'];
?>
</td>
<td>
<?php
echo $row['contact'];
?>
</td>
</tr>
<?php
else
?>
<tr>
</tr>
<?php
?>
</table>
Output:
Description: In this program, the method to upload an image using PHP is demonstrated.
The uploaded image gets stored in the specified destination.
Source Code:
if(isset($_POST['ubtn']))
$Imagename=$_FILES['img_name']['name'];
$tmpname=$_FILES['img_name']['tmp_name'];
if(move_uploaded_file($tmpname,$Imagename))
?>
<script type="text/javascript">
</script>
<?php
else
?>
<script type="text/javascript">
</script>
<?php
?>
<html>
<body>
<br>
<br>
</form>
</body>
</html>
Output: