Name: Addanki Sai Kumar
Reg.no:22MIS7236
Q1: Build a PHP script that captures a number input from
a form, computes the sum and product of its digits, and
outputs the results.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Digit Sum and Product Calculator</title>
</head>
<body>
<h2>Enter a number</h2>
<form method="POST" action="">
<input type="number" name="number" required>
<button type="submit">Submit</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$number = $_POST['number'];
$digits = str_split($number);
$sum = 0;
$product = 1;
foreach ($digits as $digit) {
$sum += $digit;
$product *= $digit;
}
echo "<h3>Results for the number: $number</h3>";
echo "Sum of digits: $sum<br>";
echo "Product of digits: $product<br>";
}
?>
</body>
</html>
Output:
Q2: Create a simple HTML form that accepts the below form data. Write a PHP
script to handle the form submission and display a output
Code:
Form.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Form</title>
</head>
<body>
<h2>Submit Your Information</h2>
<form action="handle_form.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required>
<p style="color:red;">Name can not be left blank.</p><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required>
<p style="color:red;">Email can not be left blank.</p><br>
<label for="education">Education:</label><br>
<select id="education" name="education" required>
<option value="" disabled selected>...</option>
<option value="High School">High School</option>
<option value="Bachelor's">Bachelor's</option>
<option value="Master's">Master's</option>
<option value="PhD">PhD</option>
</select>
<p style="color:red;">Tell us about your education.</p><br>
<label>Gender:</label><br>
<input type="radio" id="male" name="gender" value="Male" required> Male
<input type="radio" id="female" name="gender" value="Female" required> Female
<p style="color:red;">Specify your gender.</p><br>
<label>Hobbies:</label><br>
<input type="checkbox" name="hobbies[]" value="Drawing"> Drawing
<input type="checkbox" name="hobbies[]" value="Singing"> Singing
<input type="checkbox" name="hobbies[]" value="Dancing"> Dancing
<p style="color:red;">What are your hobbies.</p><br>
<label for="comment">Comment:</label><br>
<textarea id="comment" name="comment" rows="4" cols="50" required></textarea>
<p style="color:red;">This field is required.</p><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
form_handle.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$education = $_POST['education'];
$gender = $_POST['gender'];
$hobbies = isset($_POST['hobbies']) ? implode(", ", $_POST['hobbies']) :
"None";
$comment = $_POST['comment'];
echo "<h2>Submitted Information</h2>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Email: " . htmlspecialchars($email) . "<br>";
echo "Education: " . htmlspecialchars($education) . "<br>";
echo "Gender: " . htmlspecialchars($gender) . "<br>";
echo "Hobbies: " . htmlspecialchars($hobbies) . "<br>";
echo "Comment: " . htmlspecialchars($comment) . "<br>";
}
?>
Output:
Q3:
Form_validation.html:
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>
<h2>Form with Validation</h2>
<form action="validate_form.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" maxlength="20" required><br>
<label for="id">ID (numeric only):</label><br>
<input type="text" id="id" name="id" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password" required><br>
<label for="confirm_password">Confirm Password:</label><br>
<input type="password" id="confirm_password" name="confirm_password"
required><br>
<label for="phone">Phone Number:</label><br>
<input type="text" id="phone" name="phone" required><br>
<label for="hobbies">Hobbies:</label><br>
<input type="checkbox" name="hobbies[]" value="Drawing"> Drawing
<input type="checkbox" name="hobbies[]" value="Singing"> Singing
<input type="checkbox" name="hobbies[]" value="Dancing"> Dancing<br>
<label for="education">Education:</label><br>
<select id="education" name="education" required>
<option value="" disabled selected>...</option>
<option value="High School">High School</option>
<option value="Bachelor's">Bachelor's</option>
<option value="Master's">Master's</option>
</select><br>
<label for="date">Date (MM/DD/YYYY):</label><br>
<input type="text" id="date" name="date" placeholder="MM/DD/YYYY"
required><br>
<label for="zip">ZIP Code:</label><br>
<input type="text" id="zip" name="zip" required><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Validate_form.php:
Code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$errors = [];
// Name validation: Only letters, max length 20
$name = $_POST['name'];
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$errors[] = "Name can only contain letters and spaces.";
}
if (strlen($name) > 20) {
$errors[] = "Name can only be up to 20 characters.";
}
// ID validation: Numeric only
$id = $_POST['id'];
if (!is_numeric($id)) {
$errors[] = "ID must be numeric.";
}
// Email validation (handled by the form itself)
// Password validation: Minimum 8 characters, one uppercase, one lowercase,
one number, one special character
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if (strlen($password) < 8 || !preg_match("/[A-Z]/", $password) ||
!preg_match("/[a-z]/", $password) || !preg_match("/[0-9]/", $password) ||
!preg_match("/[\W]/", $password)) {
$errors[] = "Password must be at least 8 characters long and include at least
one uppercase letter, one lowercase letter, one number, and one special character.";
}
// Confirm Password validation: Must match password
if ($password !== $confirm_password) {
$errors[] = "Passwords do not match.";
}
// Phone number validation: Numeric, length between 10 to 15 digits
$phone = $_POST['phone'];
if (!preg_match("/^\d{10,15}$/", $phone)) {
$errors[] = "Phone number must be numeric and between 10 to 15 digits.";
}
// Checkbox validation: Ensure at least one hobby is selected
if (!isset($_POST['hobbies'])) {
$errors[] = "At least one hobby must be selected.";
}
// Dropdown validation: Ensure a valid option is selected
$education = $_POST['education'];
if ($education == "") {
$errors[] = "Please select your education.";
}
// Date validation: MM/DD/YYYY format
$date = $_POST['date'];
if (!preg_match("/^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/", $date)) {
$errors[] = "Date must be in the format MM/DD/YYYY.";
}
// ZIP Code validation: Exactly 5 digits, numeric only
$zip = $_POST['zip'];
if (!preg_match("/^\d{5}$/", $zip)) {
$errors[] = "ZIP Code must be exactly 5 numeric digits.";
}
// Display validation results
if (empty($errors)) {
echo "<h2>Form submitted successfully!</h2>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "ID: " . htmlspecialchars($id) . "<br>";
echo "Email: " . htmlspecialchars($_POST['email']) . "<br>";
echo "Phone: " . htmlspecialchars($phone) . "<br>";
echo "Education: " . htmlspecialchars($education) . "<br>";
echo "Hobbies: " . implode(", ", $_POST['hobbies']) . "<br>";
echo "Date: " . htmlspecialchars($date) . "<br>";
echo "ZIP Code: " . htmlspecialchars($zip) . "<br>";
} else {
// Display errors
echo "<h2>Errors:</h2>";
echo "<ul>";
foreach ($errors as $error) {
echo "<li>" . htmlspecialchars($error) . "</li>";
}
echo "</ul>";
echo "<a href='form_validation.html'>Go back to the form</a>";
}
}
?>
Output: