Assignment
Assignment
Practice Programs:
1. To design an application that works as a simple calculator using PHP.
👍
(use isset()).
Solution
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form method="POST">
<input type="text" name="num1" placeholder="Enter number 1">
<input type="text" name="num2" placeholder="Enter number 2">
<select name="operator">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<br>
<input type="submit" name="calculate" value="Calculate">
</form>
<?php
if (isset($_POST['calculate'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
Solution:
<!DOCTYPE html>
<html>
<head>
<title>PAN Number Validation</title>
</head>
<body>
<h1>PAN Number Validation</h1>
<form method="POST">
<input type="text" name="pan" placeholder="Enter PAN Number">
<input type="submit" name="validate" value="Validate PAN">
</form>
<?php
if (isset($_POST['validate'])) {
$pan = strtoupper($_POST['pan']); // Convert input to uppercase
// Check if the fifth character is a letter and the fourth character is valid
if (ctype_alpha($fifthChar) && in_array($fourthChar, ['A', 'B', 'C', 'F', 'G', 'H',
'J', 'L', 'P', 'T', 'Z'])) {
echo "The PAN number $pan is valid.";
} else {
echo "The PAN number is invalid. Please check the format.";
}
} else {
echo "The PAN number is invalid. Please check the format.";
}
}
?>
</body>
</html>
3. Write a PHP script to check mobile number entered by the user is valid
or not and display an appropriate message.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Mobile Number Validation</title>
</head>
<body>
<h1>Mobile Number Validation</h1>
<form method="POST">
<input type="text" name="mobile" placeholder="Enter Mobile Number">
<input type="submit" name="validate" value="Validate Mobile Number">
</form>
<?php
if (isset($_POST['validate'])) {
$mobile = $_POST['mobile'];
SET A
1. Write a PHP script to accept font name, background color, and welcome
message on 1st page. Display the welcome message with the given font and
background color on the next page.
Solution:
Page1(index.php)
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page 1</title>
</head>
<body>
<h1>Page 1: Enter Font and Background Color</h1>
<form method="POST" action="welcome.php">
<label for="font">Font Name:</label>
<input type="text" name="font" id="font" required><br><br>
page2(welcome.php)
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page 2</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$font = $_POST['font'];
$bgColor = $_POST['bgColor'];
$message = $_POST['message'];
?>
<style>
body {
background-color: <?php echo $bgColor; ?>;
font-family: <?php echo $font; ?>;
}
.welcome-message {
text-align: center;
padding: 20px;
}
</style>
<!DOCTYPE html>
<html>
<head>
<title>Form Processing</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$pincode = $_POST['pincode'];
$gender = isset($_POST['gender']) ? $_POST['gender'] : '';
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<h1>Enter Employee Details</h1>
<form method="POST" action="process_employee.php">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br><br>
<label for="address">Address:</label>
<input type="text" name="address" id="address" required><br><br>
Php:
<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$basic = $_POST['basic'];
$da = $_POST['da'];
$hra = $_POST['hra'];
SET B:
1. Write a PHP script to accept customer name and the list of product and
quantity on thefirst page. On the next page display the name of the
customer, name of the products, rateof the product, quantity, and total
price in table format.
Solution:
Page 1 (index.php):
<!DOCTYPE html>
<html>
<head>
<title>Product Order Form</title>
</head>
<body>
<h1>Enter Customer Information and Product Details</h1>
<form method="POST" action="process_order.php">
<label for="customerName">Customer Name:</label>
<input type="text" name="customerName" id="customerName" required><br><br>
<h2>Product List:</h2>
<div id="productList">
<div class="product">
<label for="productName[]">Product Name:</label>
<input type="text" name="productName[]" required>
<label for="productRate[]">Rate:</label>
<input type="number" name="productRate[]" required>
<label for="productQuantity[]">Quantity:</label>
<input type="number" name="productQuantity[]" required>
</div>
</div>
<button type="button" onclick="addProduct()">Add Product</button><br><br>
<script>
function addProduct() {
const productContainer = document.getElementById('productList');
const productDiv = document.createElement('div');
productDiv.className = 'product';
productDiv.innerHTML = `
<label for="productName[]">Product Name:</label>
<input type="text" name="productName[]" required>
<label for="productRate[]">Rate:</label>
<input type="number" name="productRate[]" required>
<label for="productQuantity[]">Quantity:</label>
<input type="number" name="productQuantity[]" required>
`;
productContainer.appendChild(productDiv);
}
</script>
</body>
</html>
Page 2 (process_order.php):
<!DOCTYPE html>
<html>
<head>
<title>Order Details</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$customerName = $_POST['customerName'];
$productNames = $_POST['productName'];
$productRates = $_POST['productRate'];
$productQuantities = $_POST['productQuantity'];
echo $tableHTML;
} else {
echo "Form not submitted.";
}
?>
</body>
</html>
2. Write HTML code to design multiple choice question paper for PHP
subject. Display question wise marks and total marks received by the
student in table format.
Solution:
Html:
<!DOCTYPE html>
<html>
<head>
<title>PHP Multiple Choice Question Paper</title>
</head>
<body>
<h1>PHP Subject - Multiple Choice Questions</h1>
<form method="post" action="grade.php">
<table border="1">
<tr>
<th>Question</th>
<th>Marks</th>
<th>Your Answer</th>
</tr>
<tr>
<td>1. What does PHP stand for?</td>
<td>2</td>
<td>
<input type="radio" name="q1" value="A"> A) Personal Home Page<br>
<input type="radio" name="q1" value="B"> B) PHP: Hypertext
Preprocessor<br>
<input type="radio" name="q1" value="C"> C) Private Hyperlink
Processor
</td>
</tr>
<tr>
<td>2. What does the PHP syntax start with?</td>
<td>1</td>
<td>
<input type="radio" name="q2" value="A"> A) <?php<br>
<input type="radio" name="q2" value="B"> B) <php<br>
<input type="radio" name="q2" value="C"> C) <?<br>
</td>
</tr>
<tr>
<td>3. How do you create a variable in PHP?</td>
<td>2</td>
<td>
<input type="radio" name="q3" value="A"> A) $variableName<br>
<input type="radio" name="q3" value="B"> B) var variableName<br>
<input type="radio" name="q3" value="C"> C) new variableName<br>
</td>
</tr>
<!-- Add more questions here -->
</table>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Php:
<!DOCTYPE html>
<html>
<head>
<title>PHP Exam Results</title>
</head>
<body>
<h1>PHP Exam Results</h1>
<?php
$totalMarks = 0;
Solution:
pagne1(index.php)
<!DOCTYPE html>
<html>
<head>
<title>Student Details</title>
</head>
<body>
<h1>Enter Student Information</h1>
<form method="POST" action="display_info.php">
<label for="studentName">Student Name:</label>
<input type="text" name="studentName" id="studentName" required><br><br>
Display_info.php
<!DOCTYPE html>
<html>
<head>
<title>Student Information</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$studentName = $_POST['studentName'];
$programmingLanguages = $_POST['programmingLanguages'];
if (count($programmingLanguages) > 0) {
echo "<ul>";
foreach ($programmingLanguages as $language) {
echo "<li>$language</li>";
}
echo "</ul>";
} else {
echo "None selected.";
}
} else {
echo "Form not submitted.";
}
?>
</body>
</html>
4. Write a PHP script to accept user name, email address and age. If data
entered by the user is valid then display it on the next page otherwise
display the appropriate message(use filter_var()).
Index.php
<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
</head>
<body>
<h1>Enter User Information</h1>
<form method="POST" action="display_info.php">
<label for="userName">User Name:</label>
<input type="text" name="userName" id="userName" required><br><br>
<label for="userEmail">Email Address:</label>
<input type="text" name="userEmail" id="userEmail" required><br><br>
<label for="userAge">Age:</label>
<input type="number" name="userAge" id="userAge" required><br><br>
Display_info.php
<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$userAge = $_POST['userAge'];
$valid = true;
// Validate age
if (!filter_var($userAge, FILTER_VALIDATE_INT, array("options" =>
array("min_range" => 1, "max_range" => 150))) === false) {
echo "Invalid age. Please enter a valid age between 1 and 150.";
$valid = false;
}
if ($valid) {
echo "<h1>User Information</h1>";
echo "User Name: $userName<br>";
echo "Email Address: $userEmail<br>";
echo "Age: $userAge years";
}
} else {
echo "Form not submitted.";
}
?>
</body>
</html>