0% found this document useful (0 votes)
32 views

Assignment

Assignments

Uploaded by

vramoshi72
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Assignment

Assignments

Uploaded by

vramoshi72
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Assignment 5: Working with forms

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'];

if (is_numeric($num1) && is_numeric($num2)) {


switch ($operator) {
case "add":
$result = $num1 + $num2;
break;
case "subtract":
$result = $num1 - $num2;
break;
case "multiply":
$result = $num1 * $num2;
break;
case "divide":
if ($num2 != 0) {
$result = $num1 / $num2;
} else {
echo "Division by zero is not allowed.";
}
break;
default:
echo "Invalid operator.";
}
echo "Result: $result";
} else {
echo "Please enter valid numeric values.";
}
}
?>
</body>
</html>

2. Write a PHP script to check PAN number entered by the customer is


valid or not and display an appropriate message.

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 PAN matches the required pattern


if (preg_match('/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/', $pan)) {
$fifthChar = $pan[4];
$fourthChar = $pan[3];

// 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'];

// Remove spaces and non-numeric characters


$mobile = preg_replace('/\D/', '', $mobile);
// Check if the mobile number is exactly 10 digits long and starts with 6, 7, 8, or 9
if (preg_match('/^[6-9][0-9]{9}$/', $mobile)) {
echo "The mobile number $mobile is valid.";
} else {
echo "The mobile number is invalid. Please check the format.";
}
}
?>
</body>
</html>

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>

<label for="bgColor">Background Color:</label>


<input type="color" name="bgColor" id="bgColor" required><br><br>

<label for="message">Welcome Message:</label>


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

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


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

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>

<h1>Page 2: Welcome Message</h1>


<div class="welcome-message">
<h2><?php echo $message; ?></h2>
</div>
<?php
} else {
echo "Please enter the font, background color, and welcome message on the
previous page.";
}
?>
</body>
</html>
2. Write a PHP program to accept name, address, pincode, gender
information. If any field is blank display error messages “all fields are
required”.
Solution:

<!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'] : '';

if (empty($name) || empty($address) || empty($pincode) || empty($gender)) {


echo "Error: All fields are required. Please fill in all the fields.";
} else {
echo "Name: $name<br>";
echo "Address: $address<br>";
echo "Pincode: $pincode<br>";
echo "Gender: $gender<br>";
}
} else {
echo "Form not submitted.";
}
?>
</body>
</html>
3. Write a PHP script to accept employee details (name, address) and
earning details (basic,DA, HRA). Display employee details and earning
details in the proper format.

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>

<label for="basic">Basic Salary:</label>


<input type="number" name="basic" id="basic" required><br><br>

<label for="da">DA (Dearness Allowance):</label>


<input type="number" name="da" id="da" required><br><br>

<label for="hra">HRA (House Rent Allowance):</label>


<input type="number" name="hra" id="hra" required><br><br>

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


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

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'];

echo "<h1>Employee Details</h1>";


echo "Name: $name<br>";
echo "Address: $address<br>";

echo "<h1>Earning Details</h1>";


echo "Basic Salary: $basic<br>";
echo "DA (Dearness Allowance): $da<br>";
echo "HRA (House Rent Allowance): $hra<br>";
} else {
echo "Form not submitted.";
}
?>
</body>
</html>

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>

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


</form>

<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'];

// Calculate total prices and build the table


$totalPrices = array();
$tableHTML = "<h1>Order Details for $customerName</h1>";
$tableHTML .= "<table border='1'><tr><th>Product
Name</th><th>Rate</th><th>Quantity</th><th>Total Price</th></tr>";
$grandTotal = 0;

for ($i = 0; $i < count($productNames); $i++) {


$productName = $productNames[$i];
$productRate = $productRates[$i];
$productQuantity = $productQuantities[$i];
$totalPrice = $productRate * $productQuantity;
$totalPrices[] = $totalPrice;
$tableHTML .=
"<tr><td>$productName</td><td>$productRate</td><td>$productQuantity</td><td>$totalP
rice</td></tr>";
$grandTotal += $totalPrice;
}

$tableHTML .= "<tr><th colspan='3'>Grand Total</th><td>$grandTotal</td></tr>";


$tableHTML .= "</table>";

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) &lt;?php<br>
<input type="radio" name="q2" value="B"> B) &lt;php<br>
<input type="radio" name="q2" value="C"> C) &lt;?<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;

if (isset($_POST['q1']) && $_POST['q1'] == 'B') {


$totalMarks += 2;
}
if (isset($_POST['q2']) && $_POST['q2'] == 'A') {
$totalMarks += 1;
}
if (isset($_POST['q3']) && $_POST['q3'] == 'A') {
$totalMarks += 2;
}
// Add more questions and grading logic here

echo "<p>Your total marks: $totalMarks</p>";


?>
</body>
</html>
3. Write a PHP script to accept student name and list of programming
languages (using drop down box) and display it on the next page in the
proper format.

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>

<label for="programmingLanguages">Select Programming Languages:</label>


<select name="programmingLanguages[]" id="programmingLanguages" multiple
required>
<option value="Java">Java</option>
<option value="Python">Python</option>
<option value="C++">C++</option>
<option value="JavaScript">JavaScript</option>
<option value="PHP">PHP</option>
<option value="Ruby">Ruby</option>
</select><br><br>

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


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

Display_info.php

<!DOCTYPE html>
<html>
<head>
<title>Student Information</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$studentName = $_POST['studentName'];
$programmingLanguages = $_POST['programmingLanguages'];

echo "<h1>Student Information</h1>";


echo "Student Name: $studentName<br>";
echo "Programming Languages: ";

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>

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


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

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 email address


if (!filter_var($userEmail, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email address. Please enter a valid email address.";
$valid = false;
}

// 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>

You might also like