web
web
code:-
<?php
$num1 = 20;
$num2 = 10;
//Addition
$add = $num1 + $num2;
echo "Adiition of $num1 and $num2 is: $add <br>";
//Subtraction
$sub = $num1 - $num2;
echo "Subtraction of $num1 and $num2 is: $sub <br>";
//Multiplication
$pro = $num1 * $num2;
echo "Multiplication of $num1 and $num2 is: $pro <br>";
//Division
$division = $num1 / $num2;
echo "Division of $num1 and $num2 is: $division <br>";
?>
Q2. Create an HTML form with fields for Name, Email, and
Age. Write a PHP script to: (03)
• Validate the input to ensure all fields are filled.
• Display an error message if validation fails.
• Show the entered data on the same page if validation is
successful.
code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
</head>
<body>
<?php
$name = $email = $age = "";
$nameErr = $emailErr = $ageErr = "";
$isValid = true;
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Validate Name
if (empty($_POST["name"]))
{
$nameErr = "Name is required";
$isValid = false;
}
else
{
$name = htmlspecialchars($_POST["name"]);
}
// Validate Email
if (empty($_POST["email"]))
{
$emailErr = "Email is required";
$isValid = false;
}
else
{
$email = htmlspecialchars($_POST["email"]);
}
// Validate Age
if (empty($_POST["age"]))
{
$ageErr = "Age is required";
$isValid = false;
}
else
{
$age = htmlspecialchars($_POST["age"]);
}
}
?>
<h1>Input Form</h1>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" value="<?php echo $name; ?>">
<span style="color: red;">* <?php echo $nameErr; ?></span><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" value="<?php echo $email; ?>">
<span style="color: red;">* <?php echo $emailErr; ?></span><br><br>
<label for="age">Age:</label><br>
<input type="number" id="age" name="age" value="<?php echo $age; ?>">
<span style="color: red;">* <?php echo $ageErr; ?></span><br><br>
code:-
<?php
// Set the default timezone
date_default_timezone_set('America/New_York'); // Change to your timezone
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Marks</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.highlight {
background-color:rgb(216, 240, 236); /* Light green background for high
marks */
}
</style>
</head>
<body>
<h1>Current Date and Time</h1>
<p>Today is: <?php echo $currentDateTime; ?></p>
<h2>Student Marks</h2>
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<?php foreach ($students as $name => $marks): ?>
<tr class="<?php echo ($marks > 80) ? 'highlight' : ''; ?>">
<td><?php echo htmlspecialchars($name); ?></td>
<td><?php echo htmlspecialchars($marks); ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>