================================================================================
  Question 29: MSc Admission Registration Web Form using CGI
================================================================================

FULL QUESTION:
Task:
Create a registration form for MSc admission and display the inserted data
on the web page.

Form Fields:
- Full Name
- Email Address
- Phone Number
- Gender
- BSc Degree / Major
- BSc Aggregate Marks (%)
- Selected MSc Specialty

SHORT QUESTION:
CGI MSc admission registration system processing form submissions into an HTML confirmation table.

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# Question 29: MSc Admission Registration Web Form using CGI

- Prerequisites: Python 3.x (Standard library urllib.parse, html).
- Running in CGI Environment: Place in /cgi-bin/ on your Apache/WAMP server.
- Running in Terminal: Run `python question_29.py` to see the generated HTML response.
- How to Run: python question_29.py
- Expected Behavior: Form parses admission details and outputs a formatted HTML confirmation card.

================================================================================
SOURCE CODE FILES:
================================================================================

1. question_29.py
--------------------------------------------------------------------------------
"""
HTML Form (Cell 98):
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MSc Admission Registration Form</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #f4f7f6; margin: 30px; }
        .form-container { max-width: 500px; background: white; padding: 20px; margin: 0 auto; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
        h2 { text-align: center; color: #2c3e50; }
        label { font-weight: bold; margin-top: 10px; display: block; }
        input, select { width: 100%; padding: 8px; margin-top: 5px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; }
        input[type="submit"] { background-color: #27ae60; color: white; border: none; padding: 10px; margin-top: 20px; cursor: pointer; font-size: 16px; }
        input[type="submit"]:hover { background-color: #219150; }
    </style>
</head>
<body>

<div class="form-container">
    <h2>MSc Admission Registration</h2>
    <form action="/cgi-bin/register.py" method="post">
        <label for="fullname">Full Name:</label>
        <input type="text" id="fullname" name="fullname" required>

        <label for="email">Email Address:</label>
        <input type="email" id="email" name="email" required>

        <label for="phone">Phone Number:</label>
        <input type="tel" id="phone" name="phone" required>

        <label for="gender">Gender:</label>
        <select id="gender" name="gender" required>
            <option value="">Select Gender</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
            <option value="Other">Other</option>
        </select>

        <label for="degree">BSc Degree / Major:</label>
        <input type="text" id="degree" name="degree" placeholder="e.g. Computer Science" required>

        <label for="percentage">BSc Aggregate (% / CGPA):</label>
        <input type="text" id="percentage" name="percentage" placeholder="e.g. 85%" required>

        <label for="msc_course">MSc Specialization:</label>
        <select id="msc_course" name="msc_course" required>
            <option value="">Select Specialization</option>
            <option value="MSc Computer Science">MSc Computer Science</option>
            <option value="MSc Data Science">MSc Data Science</option>
            <option value="MSc Information Technology">MSc Information Technology</option>
            <option value="MSc Mathematics">MSc Mathematics</option>
            <option value="MSc Physics">MSc Physics</option>
        </select>

        <input type="submit" value="Register Application">
    </form>
</div>

</body>
</html>
"""

import sys
import os
import urllib.parse
import html

print("Content-Type: text/html\n\n")

content_length = int(os.environ.get('CONTENT_LENGTH', 0))
post_data = sys.stdin.read(content_length)

parsed_data = urllib.parse.parse_qs(post_data)

fullname   = html.escape(parsed_data.get("fullname", ["Not Provided"])[0])
email      = html.escape(parsed_data.get("email", ["Not Provided"])[0])
phone      = html.escape(parsed_data.get("phone", ["Not Provided"])[0])
gender     = html.escape(parsed_data.get("gender", ["Not Provided"])[0])
degree     = html.escape(parsed_data.get("degree", ["Not Provided"])[0])
percentage = html.escape(parsed_data.get("percentage", ["Not Provided"])[0])
msc_course = html.escape(parsed_data.get("msc_course", ["Not Provided"])[0])

print(f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Registration Confirmation</title>
    <style>
        body {{ font-family: Arial, sans-serif; background-color: #f4f7f6; margin: 30px; }}
        .result-container {{ max-width: 600px; background: white; padding: 25px; margin: 0 auto; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }}
        h2 {{ color: #2c3e50; text-align: center; }}
        table {{ width: 100%; border-collapse: collapse; margin-top: 20px; }}
        th, td {{ border: 1px solid #ddd; padding: 10px; text-align: left; }}
        th {{ background-color: #2c3e50; color: white; }}
        .btn-back {{ display: inline-block; margin-top: 20px; text-decoration: none; background: #3498db; color: white; padding: 8px 15px; border-radius: 4px; }}
    </style>
</head>
<body>

<div class="result-container">
    <h2>MSc Admission - Registration Details</h2>
    <p style="color: green; text-align: center;"><b>Application Received Successfully!</b></p>

    <table>
        <tr><th>Field Name</th><th>Submitted Data</th></tr>
        <tr><td><b>Full Name</b></td><td>{fullname}</td></tr>
        <tr><td><b>Email Address</b></td><td>{email}</td></tr>
        <tr><td><b>Phone Number</b></td><td>{phone}</td></tr>
        <tr><td><b>Gender</b></td><td>{gender}</td></tr>
        <tr><td><b>BSc Degree / Major</b></td><td>{degree}</td></tr>
        <tr><td><b>BSc Aggregate Marks</b></td><td>{percentage}</td></tr>
        <tr><td><b>Selected MSc Specialty</b></td><td>{msc_course}</td></tr>
    </table>

    <a href="/index.html" class="btn-back">Submit Another Registration</a>
</div>

</body>
</html>
""")
--------------------------------------------------------------------------------
