Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views12 pages

WebTech Ass 2

The document outlines the creation of a web application for an online registration form using HTML, CSS, JavaScript, jQuery, and PHP. It includes the structure of the HTML form, styling with CSS, and functionality for form submission and data display using AJAX in JavaScript. Additionally, it provides a PHP script for processing the form data, validating inputs, and returning a JSON response upon successful registration.

Uploaded by

singhsaurabh1606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

WebTech Ass 2

The document outlines the creation of a web application for an online registration form using HTML, CSS, JavaScript, jQuery, and PHP. It includes the structure of the HTML form, styling with CSS, and functionality for form submission and data display using AJAX in JavaScript. Additionally, it provides a PHP script for processing the form data, validating inputs, and returning a JSON response upon successful registration.

Uploaded by

singhsaurabh1606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ASSIGNMENT - 2

Build a Web application with HTML, CSS, JavaScript, jQuery and PHP
for online application/registration form. Form should accept the
information and print/display on a browser with formatting/styling upon
submission (Button click) on success. Host the application on a cloud
platform.

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Registration Form</title>
<link rel="stylesheet" href="style.css">
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></
script>
</head>
<body>
<div class="container">
<h1>Online Registration Form</h1>
<form id="registrationForm" action="process.php" method="POST">
<!-- Personal Information -->
<div class="form-section">
<h2>Personal Information</h2>
<div class="form-group">
<label for="firstName">First Name*</label>
<input type="text" id="firstName" name="firstName"
required>
</div>
<div class="form-group">
<label for="lastName">Last Name*</label>
<input type="text" id="lastName" name="lastName"
required>
</div>
<div class="form-group">
<label for="dob">Date of Birth*</label>
<input type="date" id="dob" name="dob" required>
</div>
<div class="form-group">
<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>
</div>
</div>

<!-- Contact Information -->


<div class="form-section">
<h2>Contact Information</h2>
<div class="form-group">
<label for="email">Email Address*</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone Number*</label>
<input type="tel" id="phone" name="phone" required>
</div>
<div class="form-group">
<label for="address">Address*</label>
<textarea id="address" name="address"
required></textarea>
</div>
</div>

<!-- Educational Information -->


<div class="form-section">
<h2>Educational Information</h2>
<div class="form-group">
<label for="qualification">Highest
Qualification*</label>
<input type="text" id="qualification"
name="qualification" required>
</div>
<div class="form-group">
<label for="institution">Institution*</label>
<input type="text" id="institution" name="institution"
required>
</div>
<div class="form-group">
<label for="yearOfPassing">Year of Passing*</label>
<input type="number" id="yearOfPassing"
name="yearOfPassing" min="1900" max="2024" required>
</div>
</div>

<div class="form-group">
<button type="submit" id="submitBtn">Submit
Registration</button>
</div>
</form>

<!-- Result Display Section -->


<div id="resultDisplay" style="display: none;">
<h2>Registration Details</h2>
<div id="registrationDetails"></div>
<button id="printBtn">Print Details</button>
</div>
</div>

<script src="script.js"></script>
</body>
</html>

Style.css

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: Arial, sans-serif;
line-height: 1.6;
background-color: #f4f4f4;
}

.container {
max-width: 800px;
margin: 2rem auto;
padding: 2rem;
background-color: white;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

h1 {
text-align: center;
color: #333;
margin-bottom: 2rem;
}

.form-section {
margin-bottom: 2rem;
padding: 1rem;
border: 1px solid #ddd;
border-radius: 4px;
}

.form-section h2 {
color: #2c3e50;
margin-bottom: 1rem;
font-size: 1.2rem;
}

.form-group {
margin-bottom: 1rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #333;
}

input, select, textarea {


width: 100%;
padding: 0.8rem;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1rem;
}

textarea {
height: 100px;
resize: vertical;
}

button {
background-color: #3498db;
color: white;
padding: 1rem 2rem;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 1rem;
}

button:hover {
background-color: #2980b9;
}

#resultDisplay {
margin-top: 2rem;
padding: 1rem;
border: 1px solid #ddd;
border-radius: 4px;
}

#registrationDetails {
margin: 1rem 0;
}

.detail-item {
margin-bottom: 0.5rem;
padding: 0.5rem;
background-color: #f8f9fa;
border-radius: 4px;
}

.detail-label {
font-weight: bold;
color: #2c3e50;
}

@media print {
.container {
box-shadow: none;
}

button {
display: none;
}
}

@media (max-width: 768px) {


.container {
margin: 1rem;
padding: 1rem;
}
}

Script.js

$(document).ready(function() {
// Form submission handling
$('#registrationForm').on('submit', function(e) {
e.preventDefault();

// Collect form data


let formData = $(this).serialize();

// Send data to PHP script


$.ajax({
type: 'POST',
url: 'process.php',
data: formData,
dataType: 'json',
success: function(response) {
if(response.status === 'success') {
// Hide form and show results
$('#registrationForm').hide();
displayResults(response.data);
$('#resultDisplay').show();
} else {
alert('Error: ' + response.message);
}
},
error: function() {
alert('An error occurred while processing your request.');
}
});
});

// Print button functionality


$('#printBtn').on('click', function() {
window.print();
});

// Function to display results


function displayResults(data) {
let detailsHtml = '';

// Create HTML for each field


for (let key in data) {
if (data.hasOwnProperty(key)) {
detailsHtml += `
<div class="detail-item">
<span
class="detail-label">${formatLabel(key)}:</span>
<span class="detail-value">${data[key]}</span>
</div>`;
}
}

$('#registrationDetails').html(detailsHtml);
}

// Function to format label text


function formatLabel(key) {
return key
.replace(/([A-Z])/g, ' $1') // Add space before capital
letters
.replace(/^./, function(str) { return str.toUpperCase(); }) //
Capitalize first letter
.trim();
}

// Form validation
function validateForm() {
let isValid = true;

// Check required fields


$('input[required], select[required],
textarea[required]').each(function() {
if (!$(this).val()) {
isValid = false;
$(this).addClass('error');
} else {
$(this).removeClass('error');
}
});

// Validate email format


let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test($('#email').val())) {
isValid = false;
$('#email').addClass('error');
}

// Validate phone number


let phoneRegex = /^\d{10}$/;
if (!phoneRegex.test($('#phone').val())) {
isValid = false;
$('#phone').addClass('error');
}

return isValid;
}

// Add validation on input change


$('input, select, textarea').on('change', function() {
if ($(this).val()) {
$(this).removeClass('error');
}
});
});

Process.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set headers for JSON response


header('Content-Type: application/json');

// Check if the request is POST


if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode([
'status' => 'error',
'message' => 'Invalid request method'
]);
exit;
}

// Collect and sanitize form data


$formData = array(
'firstName' => sanitizeInput($_POST['firstName'] ?? ''),
'lastName' => sanitizeInput($_POST['lastName'] ?? ''),
'dob' => sanitizeInput($_POST['dob'] ?? ''),
'gender' => sanitizeInput($_POST['gender'] ?? ''),
'email' => sanitizeInput($_POST['email'] ?? ''),
'phone' => sanitizeInput($_POST['phone'] ?? ''),
'address' => sanitizeInput($_POST['address'] ?? ''),
'qualification' => sanitizeInput($_POST['qualification'] ?? ''),
'institution' => sanitizeInput($_POST['institution'] ?? ''),
'yearOfPassing' => sanitizeInput($_POST['yearOfPassing'] ?? '')
);

// Validate required fields


$requiredFields = ['firstName', 'lastName', 'dob', 'gender', 'email',
'phone', 'address', 'qualification', 'institution', 'yearOfPassing'];
foreach ($requiredFields as $field) {
if (empty($formData[$field])) {
echo json_encode([
'status' => 'error',
'message' => ucfirst($field) . ' is required'
]);
exit;
}
}

// Validate email
if (!filter_var($formData['email'], FILTER_VALIDATE_EMAIL)) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid email format'
]);
exit;
}
// Validate phone number
if (!preg_match("/^\d{10}$/", $formData['phone'])) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid phone number format'
]);
exit;
}

// If all validations pass, return success response


echo json_encode([
'status' => 'success',
'message' => 'Registration successful',
'data' => $formData
]);

// Function to sanitize input


function sanitizeInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
WEBPAGE:-

You might also like