You are on page 1of 5

6.

WAP to create web page for hostel registration information for student and
validate it at server side. (Remove white spaces from the input data).

<!DOCTYPE html>
<html>
<head>
<title>Hostel Registration Form</title>
</head>
<body>
<h1>Hostel Registration Form</h1>
<form action="process_form.php" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>

<label for="roll">Roll Number:</label><br>


<input type="text" id="roll" name="roll" required><br><br>

<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

process_form.php

<?php
$name = trim($_POST['name']);
$roll = trim($_POST['roll']);
$email = trim($_POST['email']);
$errors = [];
if (empty($name) || empty($roll) || empty($email))
{
$errors[] = "All fields are required.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = "Invalid email format.";
}

if (!empty($errors))
{
foreach ($errors as $error)
{
echo $error . "<br>";
}
}
else
{
echo "Registration successful!<br>";
echo "Name: " . $name . "<br>";
echo "Roll Number: " . $roll . "<br>";
echo "Email: " . $email . "<br>";
}

?>
7. WAP of Method Overloading for area of circle and rectangle using magic
methods in PHP.

<?php
class shape
{
function __call($name_of_function, $arguments)
{
If($name_of_function == 'area')
{
switch (count($arguments))
{
case 1:
return 3.14 * $arguments[0];
case 2:
return $arguments[0]*$arguments[1];
}
}
}
}
$s = new Shape;
echo($s->area(2)); //area of circle

echo "\n";
echo ($s->area(4, 2)); //area of rectangle

?>
8. WAP to demonstrate constructor & destructor in a class.

<?php

class Student
{
public $name;
function __construct($name)
{
$this->name = $name;
}
function __destruct()
{
echo "<h4>The Student record with name '{$this->name}'
is removed from the database.";
}
}

$s = new Student("Ashwini");

?>
9. WAP for counting page refresh using cookies in PHP

<?php
function setPageRefreshCount()
{
if (isset($_COOKIE['page_refresh_count']))
{
$count = $_COOKIE['page_refresh_count'] + 1;
}
else
{
$count = 1;
}
setcookie('page_refresh_count', $count, time() + 3600);
}
if (!isset($_COOKIE['page_refresh_count']))
{
setPageRefreshCount();
}

// Display the page refresh count


echo "Page refresh count: " . $_COOKIE['page_refresh_count'];

?>

You might also like