Assignments For Class 11 Forms and User Input in PHP

Rashmi Mishra
0

Assignments For  Class 11  

Forms and User Input in PHP

Here some assignments focused on handling forms and user input in PHP, covering form creation, validation, sanitization, and data handling using $_GET and $_POST.


Assignment 1: Create a Registration Form

  1. Objective: Design a basic user registration form that collects the following information:
    • Username
    • Password
    • Email address
    • Age
  2. Instructions:
    • Create an HTML form with the required fields.
    • Use the POST method to send data to a register.php file.
    • In register.php, retrieve and display the user input.
  3. Requirements:
    • Ensure all fields are filled out.
    • Validate that the email has a valid format and the age is a numeric value.
    • Sanitize all inputs before displaying them back to the user.

Assignment 2: Feedback Form with Validation

  1. Objective: Build a feedback form where users can provide their feedback and rate their experience.
  2. Instructions:
    • Create a form that includes:
      • Name
      • Email
      • Rating (from 1 to 5)
      • Feedback message (text area)
    • Set the action attribute to submit to feedback.php with the POST method.
  3. Requirements:
    • Validate that:
      • Name and email are required fields.
      • The email has a correct format.
      • The rating is between 1 and 5.
    • Sanitize the input data and display it back in feedback.php with a thank-you message.

Assignment 3: Simple Login Form

  1. Objective: Create a simple login form to collect a username and password, and validate the inputs.
  2. Instructions:
    • Design a form that collects a username and password with the POST method.
    • Create a PHP script (login.php) that:
      • Validates that both fields are filled.
      • Checks if the username is at least 5 characters long.
      • Sanitizes both inputs before displaying them back to the user.
  3. Requirements:
    • If validation fails, display appropriate error messages.
    • If validation passes, display a welcome message that includes the username.

Assignment 4: Contact Form with Sanitization

  1. Objective: Develop a contact form that gathers user details securely.
  2. Instructions:
    • Design a form with fields for:
      • Full Name
      • Email
      • Subject
      • Message (text area)
    • Use the POST method to send the data to a contact.php file.
  3. Requirements:
    • Validate that:
      • Full Name and Email are required.
      • The email format is valid.
    • Sanitize the inputs to prevent HTML injection using htmlspecialchars().
    • Display a confirmation message showing the sanitized data.

Assignment 5: Student Registration Form with Conditional Validation

  1. Objective: Create a student registration form with conditional validation for optional fields.
  2. Instructions:
    • Create a form that includes:
      • Full Name
      • Email
      • Age
      • Phone number (optional)
    • Use POST to submit to student_register.php.
  3. Requirements:
    • Validate that:
      • Full Name and Email are required.
      • The email format is valid.
      • Age, if provided, is a number between 18 and 60.
    • Sanitize inputs, display any error messages if validation fails, and show the sanitized data if validation succeeds.

Assignment 6: User Feedback Form with GET Method

  1. Objective: Build a simple user feedback form that uses the GET method to send data.
  2. Instructions:
    • Design a form with:
      • Name
      • Comments (text area)
    • Set method="get" and action="feedback_get.php".
  3. Requirements:
    • Sanitize both Name and Comments fields to prevent harmful input.
    • Display the feedback message along with the sanitized name in feedback_get.php.
    • Note that GET appends data to the URL; keep the feedback short.

Assignment 7: Password Validation in Registration Form

  1. Objective: Extend the registration form with password validation rules.
  2. Instructions:
    • Create a registration form that includes:
      • Username
      • Password
      • Confirm Password
    • Set method="post" and submit to register_password.php.
  3. Requirements:
    • Validate that:
      • The password is at least 8 characters long.
      • The Password and Confirm Password fields match.
    • If validation passes, display a success message with the sanitized username. If validation fails, display error messages indicating the issues.

 Assignment 8: Event Registration Form

Objective: Create a simple event registration form that captures user details and validates the inputs.

Requirements:

  • The form should include fields for name, email, event date, and number of participants.
  • Validate the email format, check that the date is not in the past, and ensure the number of participants is a positive integer.

Assignment 9: Job Application Form

Objective: Create a job application form that captures user details and validates the inputs.

Requirements:

  • The form should include fields for full name, email, phone number, resume (file upload), and cover letter.
  • Validate the email format and check that the phone number consists of digits only.

 Assignment 10: Newsletter Subscription Form

Objective: Create a newsletter subscription form that captures user details and validates the inputs.

Requirements:

  • The form should include fields for name and email.
  • Validate the email format and ensure that the name is not empty.

Assignment 11: User Profile Update Form

Objective: Create a user profile update form that allows users to update their information.

Requirements:

  • The form should include fields for username, email, and phone number.
  • Validate the email format and check that the phone number consists of digits only.

Assignment 12: Product Review Form

Objective: Create a product review form that allows users to submit reviews for products.

Requirements:

  • The form should include fields for product name, review, and rating (1 to 5).
  • Validate that the rating is between 1 and 5.

Solution  

Assignment 1: Registration Form

index.html:

<form action="register.php" method="POST">

  Username: <input type="text" name="username" required><br>

  Password: <input type="password" name="password" required><br>

  Email: <input type="email" name="email" required><br>

  Age: <input type="number" name="age" required><br>

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

</form>

 

 register.php:

<?php

// Retrieve and sanitize form inputs

$username = htmlspecialchars($_POST['username']);

$password = htmlspecialchars($_POST['password']);

$email = htmlspecialchars($_POST['email']);

$age = htmlspecialchars($_POST['age']);

 

// Validate email and age

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif (!is_numeric($age)) {

    echo "Age must be a number.<br>";

} else {

    echo "Registration Successful!<br>";

    echo "Username: $username<br>Email: $email<br>Age: $age";

}

?>           


 

Assignment 2: Feedback Form with Validation

feedback.html:

<form action="feedback.php" method="POST">

  Name: <input type="text" name="name" required><br>

  Email: <input type="email" name="email" required><br>

  Rating: <input type="number" name="rating" min="1" max="5" required><br>

  Feedback: <textarea name="feedback" required></textarea><br>

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

</form>

 

 feedback.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

$rating = htmlspecialchars($_POST['rating']);

$feedback = htmlspecialchars($_POST['feedback']);

 

// Validate email and rating

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif ($rating < 1 || $rating > 5) {

    echo "Rating must be between 1 and 5.<br>";

} else {

    echo "Thank you for your feedback, $name!<br>";

    echo "Rating: $rating<br>Feedback: $feedback";

}

?>

 

 Assignment 3: Simple Login Form

login.html:

<form action="login.php" method="POST">

  Username: <input type="text" name="username" required><br>

  Password: <input type="password" name="password" required><br>

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

</form>

 

 login.php:

<?php

// Sanitize inputs

$username = htmlspecialchars($_POST['username']);

$password = htmlspecialchars($_POST['password']);

 

// Validate username length

if (strlen($username) < 5) {

    echo "Username must be at least 5 characters long.<br>";

} else {

    echo "Welcome, $username!";

}

?>

 

 Assignment 4: Contact Form with Sanitization

contact.html:

<form action="contact.php" method="POST">

  Full Name: <input type="text" name="name" required><br>

  Email: <input type="email" name="email" required><br>

  Subject: <input type="text" name="subject" required><br>

  Message: <textarea name="message" required></textarea><br>

  <input type="submit" value="Send Message">

</form>

 

 contact.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

$subject = htmlspecialchars($_POST['subject']);

$message = htmlspecialchars($_POST['message']);

 

// Validate email

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} else {

    echo "Message Sent!<br>";

    echo "Name: $name<br>Email: $email<br>Subject: $subject<br>Message: $message";

}

?>

 

 Assignment 5: Student Registration Form with Conditional Validation

student_register.html:

<form action="student_register.php" method="POST">

  Full Name: <input type="text" name="name" required><br>

  Email: <input type="email" name="email" required><br>

  Age: <input type="number" name="age"><br>

  Phone: <input type="text" name="phone"><br>

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

</form>

 

 student_register.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

$age = htmlspecialchars($_POST['age']);

$phone = htmlspecialchars($_POST['phone']);

 

// Validate email and age

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif ($age && ($age < 18 || $age > 60)) {

    echo "Age must be between 18 and 60 if provided.<br>";

} else {

    echo "Registration Successful!<br>";

    echo "Name: $name<br>Email: $email<br>Age: $age<br>Phone: $phone";

}

?>

 

 Assignment 6: User Feedback Form with GET Method

feedback_get.html:

<form action="feedback_get.php" method="GET">

  Name: <input type="text" name="name" required><br>

  Comments: <textarea name="comments" required></textarea><br>

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

</form>

 

 feedback_get.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_GET['name']);

$comments = htmlspecialchars($_GET['comments']);

 

// Display sanitized feedback

echo "Thank you, $name, for your feedback!<br>";

echo "Your comments: $comments";

?>

 

 Assignment 7: Password Validation in Registration Form

register_password.html:

<form action="register_password.php" method="POST">

  Username: <input type="text" name="username" required><br>

  Password: <input type="password" name="password" required><br>

  Confirm Password: <input type="password" name="confirm_password" required><br>

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

</form>

 

 register_password.php:

<?php

// Sanitize inputs

$username = htmlspecialchars($_POST['username']);

$password = $_POST['password'];

$confirm_password = $_POST['confirm_password'];

 

// Validate password length and matching

if (strlen($password) < 8) {

    echo "Password must be at least 8 characters long.<br>";

} elseif ($password !== $confirm_password) {

    echo "Passwords do not match.<br>";

} else {

    echo "Registration Successful!<br>";

    echo "Username: $username";

}

?>

 

 Assignment 8: Event Registration Form

  1. event_registration.html:

<form action="event_registration.php" method="POST">

  Name: <input type="text" name="name" required><br>

  Email: <input type="email" name="email" required><br>

  Event Date: <input type="date" name="event_date" required><br>

  Number of Participants: <input type="number" name="participants" min="1" required><br>

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

</form>

 event_registration.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

$event_date = $_POST['event_date'];

$participants = htmlspecialchars($_POST['participants']);

 

// Validate email and event date

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif (strtotime($event_date) < time()) {

    echo "Event date must be in the future.<br>";

} elseif (!is_numeric($participants) || $participants < 1) {

    echo "Number of participants must be a positive integer.<br>";

} else {

    echo "Registration Successful!<br>";

    echo "Name: $name<br>Email: $email<br>Event Date: $event_date<br>Participants: $participants";

}

?>


 

 

Assignment 9: Job Application Form

  1. job_application.html:

<form action="job_application.php" method="POST" enctype="multipart/form-data">

  Full Name: <input type="text" name="full_name" required><br>

  Email: <input type="email" name="email" required><br>

  Phone Number: <input type="text" name="phone" required><br>

  Resume: <input type="file" name="resume" accept=".pdf,.doc,.docx" required><br>

  Cover Letter: <textarea name="cover_letter" required></textarea><br>

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

</form>

 

 job_application.php:

<?php

// Sanitize inputs

$full_name = htmlspecialchars($_POST['full_name']);

$email = htmlspecialchars($_POST['email']);

$phone = htmlspecialchars($_POST['phone']);

$cover_letter = htmlspecialchars($_POST['cover_letter']);

 

// Validate email and phone number

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif (!preg_match('/^[0-9]+$/', $phone)) {

    echo "Phone number must contain digits only.<br>";

} else {

    // Handle file upload

    $resume = $_FILES['resume'];

    $upload_directory = 'uploads/';

    $file_path = $upload_directory . basename($resume['name']);

   

    if (move_uploaded_file($resume['tmp_name'], $file_path)) {

        echo "Application Submitted Successfully!<br>";

        echo "Full Name: $full_name<br>Email: $email<br>Phone: $phone<br>Resume uploaded to: $file_path<br>Cover Letter: $cover_letter";

    } else {

        echo "Error uploading resume.<br>";

    }

}

?>

 

 Assignment 10: Newsletter Subscription Form

  1. newsletter_subscription.html:

<form action="newsletter_subscription.php" method="POST">

  Name: <input type="text" name="name" required><br>

  Email: <input type="email" name="email" required><br>

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

</form> 

 newsletter_subscription.php:

<?php

// Sanitize inputs

$name = htmlspecialchars($_POST['name']);

$email = htmlspecialchars($_POST['email']);

 

// Validate email

if (empty($name)) {

    echo "Name cannot be empty.<br>";

} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} else {

    echo "Thank you for subscribing, $name!<br>Your email: $email";

}

?>


 

 

Assignment 11: User Profile Update Form

  1. profile_update.html

<form action="profile_update.php" method="POST">

  Username: <input type="text" name="username" required><br>

  Email: <input type="email" name="email" required><br>

  Phone Number: <input type="text" name="phone" required><br>

  <input type="submit" value="Update Profile">

</form>

 

 profile_update.php:

<?php

// Sanitize inputs

$username = htmlspecialchars($_POST['username']);

$email = htmlspecialchars($_POST['email']);

$phone = htmlspecialchars($_POST['phone']);

 

// Validate email and phone number

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    echo "Invalid email format.<br>";

} elseif (!preg_match('/^[0-9]+$/', $phone)) {

    echo "Phone number must contain digits only.<br>";

} else {

    echo "Profile Updated Successfully!<br>";

    echo "Username: $username<br>Email: $email<br>Phone: $phone";

}

?>


 

 

 

Assignment 12: Product Review Form

  1. product_review.html:

<form action="product_review.php" method="POST">

  Product Name: <input type="text" name="product_name" required><br>

  Review: <textarea name="review" required></textarea><br>

  Rating: <input type="number" name="rating" min="1" max="5" required><br>

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

</form>

 

 product_review.php:

<?php

// Sanitize inputs

$product_name = htmlspecialchars($_POST['product_name']);

$review = htmlspecialchars($_POST['review']);

$rating = htmlspecialchars($_POST['rating']);

 

// Validate rating

if ($rating < 1 || $rating > 5) {

    echo "Rating must be between 1 and 5.<br>";

} else {

    echo "Thank you for your review!<br>";

    echo "Product Name: $product_name<br>Review: $review<br>Rating: $rating";

}

?>


 

 


Post a Comment

0Comments

Post a Comment (0)